【问题标题】:How to exclude from another QuerySet?如何从另一个 QuerySet 中排除?
【发布时间】:2013-06-27 11:22:14
【问题描述】:

我有疑问:

users_to_exclude = MyModel.objects.filter(status=1)

现在:

result = MyAnotherModel.objects.filter(image=my_image).count()

和:

result = MyAnotherModel.objects.filter(image=my_image).exclude(user__in=users_to_exclude).count()

显示相同的数字(users_to_exclude 有正确的数据)。如何做好?

【问题讨论】:

  • user_to_exclude是什么类型的?
  • MyModel.objects.filter(status=1) 返回类型是 MyModel 查询集。 exclude(user__in=users_to_exclude) 期望的是用户类型的查询集。因此问题。 MyModel 里面有用户对象吗?
  • 显示模型定义

标签: django django-queryset


【解决方案1】:

阅读该主题:

https://docs.djangoproject.com/en/dev/ref/models/querysets/#in

还有:

You can also use a queryset to dynamically evaluate the list of values instead of providing a list of literal values:

【讨论】:

    猜你喜欢
    • 2012-04-28
    • 2019-12-12
    • 1970-01-01
    • 2020-10-01
    • 2018-07-18
    • 2014-04-11
    • 2016-02-16
    • 2011-09-08
    • 2020-02-24
    相关资源
    最近更新 更多