Search code examples
djangodjango-filterdjango-filters

Django filter OR .filter(x=1,y=2) like Django Docu


So this works:

from django.db.models import Q

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

according to: https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships

this should give me the same as the first code snippet

Item.objects.filter(creator=owner, moderated=False)

but it acts like:

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

Edit: I misunderstood that the 2nd and 3rd snippet are the same unless Many-To-Many Fields come into place. But both have nothing to do with the first snippet


Solution

  • Q with operator | can be translated into:

    Please give me all objects of instance Item which creator=owner OR moderated=False

    (OR is important here)

    When .filter(creator=owner, moderated=False) says:

    Please give me all objects of instace Item which creator=owner AND moderated=False