我有以下示例:
class Profile(models.Model):
...
class Person(models.Model):
profile = models.ForeignKey(Profile, ...)
Run Code Online (Sandbox Code Playgroud)
我有复杂的 Profile 类模型管理器,并且我构建了一个视图来列出大量人员。我尝试计算数据库中的所有内容,因此我想从 Person QuerySet 调用配置文件管理器。
为此,我需要执行以下操作:
Person.objects.filter(...).select_related('profile', queryset=Profile.objects.with_computed_revenue().all())
Run Code Online (Sandbox Code Playgroud)
然后我应该能够从 SQL 中检索 person.profile.compulated_revenue,其中函数“with_compulated_revenue”是 ProfileManager 的一个函数,用于注释compulated_revenue。
最终目标是添加亲自查询集:
.values('profile__computed_revenue')
Run Code Online (Sandbox Code Playgroud)
使用 Prefetch 来实现 prefetch_lated 似乎是可能的,但我找不到与 select_lated 等效的内容。
django ×1