【问题标题】:Exclude issue to get the count with a subquery排除问题以使用子查询获取计数
【发布时间】:2013-05-10 10:43:59
【问题描述】:

我有两个模型。

物品

class Item:
   name = Charfield

class ItemCopy:
   orig = foreignkey(item)
   Copy = foregin key (item)

所以我想获取所有项目的计数,但如果项目 ID 存在于 ItemCopy.copy 中,我想排除。 Item 与 ItemCopy 没有关联,但 ItemCopy 与 Item 有关联。

可能是这样的:

Item.objects.exclude(itemCopy=item.id).count()

有人知道如何排除它吗?

谢谢

【问题讨论】:

    标签: django django-models django-orm


    【解决方案1】:

    我现在用子查询通过这种方式解决了它。

    Item.objects.exclude(id__in=ItemCopy.objects.values_list("copy", flat=True)).count()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多