【发布时间】:2020-11-24 13:36:25
【问题描述】:
我有BottleType 和OrganisationBottleType 模型。 我想通过来自 OrganisationBottleType 的字段 'is_accepted' 和 'points' 来注释 BottleType 查询集。
OrganisationBottleType 模型:
class OrganisationBottleType(models.Model):
organisation = models.ForeignKey(
'Organisation',
related_name='organisation_bottle_types',
on_delete=models.CASCADE,
)
bottle_type = models.ForeignKey(
'machines.BottleType',
related_name='organisation_bottle_types',
on_delete=models.CASCADE,
)
is_accepted = models.BooleanField(...)
points = models.FloatField(...)
假设我有 organization_id 和 BottleType 查询集,因此对于查询集中的每个对象,需要找到按瓶类型和组织过滤的 OrganisationBottleType 并注释字段“is_accepted”和“点”。
(当找到过滤的OrganisationBottleType qs 时可以从中获取第一个对象,因为假设这两个字段:organization - bottle_type 是唯一的)
我的想法是在注释中使用子查询,但我做不到。
我将不胜感激!
【问题讨论】:
标签: django django-models django-queryset m2m