我正在構建一個酒店管理系統,我已經編寫了一個 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
提交
取消