Django object multiple exclude()

后端 未结 5 2196
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 22:20

Is there a way to do a query and exclude a list of things, instead of calling exclude multiple times?

5条回答
  •  星月不相逢
    2020-12-30 22:36

    To improve on Daniel Roseman's answer I think it would be better to get the values you need directly from the queryset instead of the for loop that could be expensive on large data sets i.e.

    names_to_exclude = objects_to_exclude.values_list('name')
    Foo.objects.exclude(name__in=names_to_exclude)
    

提交回复
热议问题