简体   繁体   中英

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

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM