How do I do an OR filter in a Django query?

Mez picture Mez · Apr 11, 2009 · Viewed 167.4k times · Source

I want to be able to list the items that either a user has added (they are listed as the creator) or the item has been approved.

So I basically need to select:

item.creator = owner or item.moderated = False

How would I do this in Django? (preferably with a filter or queryset).

Answer

Alex Koshelev picture Alex Koshelev · Apr 11, 2009

There is Q objects that allow to complex lookups. Example:

from django.db.models import Q

Item.objects.filter(Q(creator=owner) | Q(moderated=False))