如何从另一个QuerySet中排除?

时间:2013-06-27 11:22:15

标签: django django-queryset

我有疑问:

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有正确的数据)。怎么做得好?

1 个答案:

答案 0 :(得分: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: