0

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

1

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

Comments

2

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

4 Comments

Both answers are right, but this one uses references and so should be accepted
yeah, move your Q statements to the last
@Jharwood the above answer uses reference too .. Hower over Q
oh, it wasn't obvious it was a link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.