user1529342
user1529342

Reputation: 354

Using OR operation in Django query

HI i am trying to use OR operator in django query. my code looks like:

hotels = models.Hotel.objects.filter(
        wed=True,
        county=hotel_main.county.id,
        (x=True)|(y=True)|(z=True),
        subscriptions__end_date__gte=datetime.date.today(),
        subscriptions__enquiry_count__lte=F('subscriptions__tier__enquiry_limit'),
    ).distinct()

I am trying to fetch a record having atleast one of the above x, y, z as true. Any help ...thank you

Upvotes: 0

Views: 187

Answers (2)

karthikr
karthikr

Reputation: 99670

You would have to use the Q object. Something like this:

hotels = models.Hotel.objects.filter(
    wed=True,
    county=hotel_main.county.id,
    subscriptions__end_date__gte=datetime.date.today(),
    subscriptions__enquiry_count__lte=F('subscriptions__tier__enquiry_limit')).filter( Q(x=True) | Q(y=True) | Q(z=True)).distinct()

Upvotes: 2

Vivek S
Vivek S

Reputation: 5550

Q objects

Contact.objects.filter(Q(last_name__icontains=request.POST['query']) | 
                           Q(first_name__icontains=request.POST['query']))

REF: OR operator in Django model queries

Upvotes: 2

Related Questions