djangodjango-queryset

How to do an OR filter in a Django query?


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).


Solution

  • 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))