django objects.filter with 2 conditions for the same keyword where either of them match

django, python

Solution

Without more detail into what fields you're matching it's hard to be exact, but basically you want to use the `|` operator to combine two Q objects.

from django.db.models import Q
result = SomeModel.objects.filter(Q(somefield='foo') | Q(somefield='bar'))

See the docs for full details on Q objects: https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects

Problem

I wish to filter the queryset to contain objects satisfying either of the 2 conditions. I saw the example where both the conditions are matched, but I wish either of the condition is matched. How do I do it using Q object? enter link description here

Original source

Related problems