我正在构建一个酒店管理系统,我已经编写了一个 check_availability 函数,该函数将根据客户要求的类别检查房间是否为空,但事实证明该函数忽略了数据库,因为它只关心现有的一个不是添加的一个。这是代码:从 django.db 导入模型from django.contrib.auth.models import Userclass RoomCategory(models.Model): name = models.CharField(max_length=59) price = models.IntegerField() beds = models.PositiveIntegerField() capacity = models.PositiveIntegerField() size = models.CharField(max_length=59) def __str__(self): return self.nameclass Room(models.Model): room_number = models.CharField(max_length=60) room_category = models.ForeignKey(RoomCategory, on_delete=models.CASCADE) def __str__(self): return f"The room {self.room_number} {self.room_category} has a maximum of {self.room_category.capacity} person and cost {self.room_category.price}/night " class Booking(models.Model): customer = models.ForeignKey(User, on_delete=models.CASCADE) room = models.ForeignKey(Room, on_delete=models.CASCADE) check_in = models.DateTimeField() check_out = models.DateTimeField() def __str__(self): return f"{self.customer} has booked for a {self.room} room from {self.check_in} to {self.check_out}" from django.shortcuts import renderfrom .forms import BookingFormfrom .models import Booking, RoomCategory, Roomfrom django.views.generic import FormView from Hotel.Availabililty.Available import check_availabilityfrom django.http import HttpResponse# Create your views here.class BookingFormview(FormView): form_class = BookingForm template_name = 'Hotel/bookingformview.html' )
1 回答
心有法竹
TA贡献1866条经验 获得超5个赞
Room您可以获得不存在重叠的 s列表Booking:
roomlist = Room.objects.filter(
room_category__name=data['room']
).exclude(
booking__check_in__lte=data['check_out'],
booking__check_out__gte=data['check_in']
)
逻辑如下,如果a 1 > b 2或b 1 < a 2 ,两个区间[a 1 , b 1 ]和[ a 2 , b 2 ]不重叠。因此我们可以否定逻辑,因此如果a 1 ≤b 2且b 1 ≥a 2则两个范围合计。
添加回答
举报
0/150
提交
取消