[Django]-Django objects.filter with 2 conditions for the same keyword where either of them match

7👍

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

Leave a comment