I have the following models.
class Room(models.Model):
room_number = models.PositiveSmallIntegerField(
validators=[MaxValueValidator(1000), MinValueValidator(1)],
primary_key=True
)
class TimeSlot(models.Model):
room = models.ForeignKey(Room, on_delete=models.CASCADE)
available_from = models.TimeField()
available_till = models.TimeField()
Here is my views.py
keys = ['room', 'available_from_gte', 'available_till_lte']
values = [room_obj, available_from, available_till]
# room_obj contains object of Room model. available_from and available_till contains time in 02:00:00 format.
parameters = {}
for key, value in zip(keys, values):
if value is not None and value !=[] and value != '':
parameters[key] = value
time_slots_list = TimeSlot.objects.filter(**parameters)
On running the above code I get the following error.
Cannot resolve keyword 'available_from_gte' into field. Choices are: available_from, available_till, id, room, room_id
Can someone please help me with it?
CodePudding user response:
I believe what you are looking for is available_from__gte
. Notice the double underscore before gte. Accessing into related fields and lookups such as in
and gte
are notated with two underscores in field names.