Home > Net >  Use and, or conditions while using Case,When
Use and, or conditions while using Case,When

Time:02-05

Anyone know in the following query how can I use AND condition?

q = Food.objects.all().annotate(open=Case(When(days__day=today,then='blahblah'),When(start_time__lte=now and end_time__gte=now,then='blabla')))

On the second when I want to check if the now value is between start and end time but it seems like the 'and' keyword is not working there

CodePudding user response:

Use Q objects so:

from django.db.models import F, Q, When

q = Food.objects.annotate(open=Case(
    When(days__day=today, then='blahblah'),
    When(Q(start_time__lte=now) & Q(end_time__gte=now), then='blabla'),
)

Use & to AND two Q objects and | to OR them.

  • Related