Django object multiple exclude()

Johnd picture Johnd · May 26, 2009 · Viewed 22.4k times · Source

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

Answer

Daniel Roseman picture Daniel Roseman · May 26, 2009

Based on your reply to Ned, it sounds like you just want to exclude a list of tags. So you could just use the in filter:

names_to_exclude = [o.name for o in objects_to_exclude] 
Foo.objects.exclude(name__in=names_to_exclude)

Does that do what you want?