JavaCake
JavaCake

Reputation: 4115

How to filter based on either one of the columns

I have a table consisting of two columns: col1 and col2. If myValue exist in either col1 or col2 it should be added to my queryset.

Using:

Table.objects.all().filter(col1=myValue).filter(col2=myValue)

Seems to just concatenate the filters and basically results in a empty queryset, as myValue cannot occur in both col1 and col2. So how do i do a OR filtering?

Upvotes: 0

Views: 51

Answers (1)

falsetru
falsetru

Reputation: 369074

You can use django.db.models.Q which can be combine using | (OR).

from django.db.models import Q

...

Table.objects.filter(Q(col1=myValue) | Q(col2=myValue))

Upvotes: 1

Related Questions