1👍
✅
You can make use of Django’s Case to perform conditional expressions
Give this a try:
from django.db.models import Case, When, Q, F
from datetime import datetime
now = datetime.now()
qs = TimeInfo.objects.annotate(
next_day=Case(
When(Q(close_time__lt=now), then=F('open_time') + timedelta(days=1)),
default=F('open_time'),
output_field=models.DateTimeField()
)
).filter(
Q(open_time__lte=now, close_time__gte=now) | Q(next_day__lte=now),
month='25 марта', # change this to actual month name
day_of_week='Суббота', # change this to actual day of week name
locality=79 # change this with actual locality id
).order_by('-close_time')
# to get the first result
result = qs.first()
Source:stackexchange.com