1

假设我有这个模型:

Class Item(models.Model):
    ...

Class ItemCollection(models.Model):
    ...
    items = models.ManyToManyField(Item)
    ...

现在我过滤 ItemCollection:

collection = RuleRequest.objects.filter(*some_filter*)

现在从“集合”查询集中,我需要从 ManyToManyField 中获取所有唯一项目。这对于单个对象很容易完成,但是如何使用查询集呢?

4

1 回答 1

0

不确定这是否是您要问的...但是如果您只是想获得由 ItemCollection 过滤的唯一项目,则波纹管应该可以工作:

Item.objects.filter(itemcollection__*somefilter*).distinct()

例如,如果 ItemCollection 有活动字段

Item.objects.filter(itemcollection__active=True).distinct()
于 2011-07-14T10:44:59.967 回答