Reputation: 77
I have a problem with lookup that looks for a value in related set.
class Room(models.Model):
name = models.Charfield(max_lentgh=64)
class Availability(models.Model):
date = models.DateField()
closed = models.BooleanField(default=False)
room = models.ForeignKey(Room)
Considering that there is one availability for every date in a year. How can use ORM query to find whether room withing given daterange (i.e. 7 days) has:
I was unable to find any orm examples that check whether all objects within daterange exist
Upvotes: 1
Views: 57
Reputation: 476750
You can enumerate over the date
s, and ensure that it has for that date
an Availability
with closed=False
:
from datetime import date, timedelta
rooms = Room.objects.all()
start_date = date(2022, 7, 21) # first day
for dd in range(7): # number of days
dt = start_date + timedelta(days=dd)
rooms = rooms.filter(availability__date=dt, availability__closed=False)
The rooms
will after the for
loop have a QuerySet
with all Room
s that have for all date
s in that range Availability
objects with closed=False
.
Upvotes: 1