我正在开发酒店预订应用程序,其中我有一个“RoomType”模型,其中包含每天可用房间数的字段
我创建了一个用于保存BookEdates(from,to)的新模型,其中ForiegnKey用于RoomType模型
每次用户预订房间(从、到),都会发送一个信号,将RoomType模型保存在其中的许多字段中
现在,最终用户将搜索特定日期(从、到)的可用房间
因此,我创建了一个查询,以筛选此日期内可用的房间,并检查其在特定日期内的保留次数是否超过每天可用eRoom的数量
我的问题是当我查询筛选RoomTypes i.exclude()时,通过它查询BookedDates模型,其中NumberOfAvailableRoomper day字段小于该日期预订的特定于时间的房间数。。。 但实际上,这样做的结果不包括BookEdates模型中预订的任何房间类型
models.py
class BookedDates(models.Model):
hotel = models.ForeignKey(Hotel, on_delete=models.CASCADE, null=True)
room_type = models.ForeignKey(
'RoomType', on_delete=models.CASCADE, null=True)
booked_from = models.DateField(null=True)
booked_to = models.DateField(null=True)
def __str__(self):
return '{} /\ {} /\ {}'.format(self.room_type, self.hotel, self.booked_from)
class RoomType(models.Model):
hotel = models.ForeignKey(Hotel, on_delete=models.CASCADE)
room_type = models.ForeignKey(RoomTypesNames, on_delete=models.CASCADE)
bookedDates = models.ManyToManyField(
BookedDates)
number_of_rooms = models.PositiveIntegerField()
views.py
roomsByDate = RoomType.objects.annotate(modulo=numAdult % F('room_capacity')).filter(
q_city, q_room_capacity1, modulo=0
).exclude(number_of_rooms__lte = BookedDates.objects.filter(room_type=F('room_type'),
booked_from__in = day).count()) ## my error is that the exclude , excludes any room type in the BookedDates model, even if it is reserved in this date number of times less than number_of_rooms available per day
欢迎以正确方式创建此查询的任何其他方法提前感谢
谢谢你的@schillingt
我相信您只需要另一个注释来计算该房间的预订数量。您可以使用反向外键路径/相关名称,在本例中,我认为它是
roomtype_set
相关问题 更多 >
编程相关推荐