如何编写一个函数来检查 django 中酒店的空房间

我正在构建一个酒店管理系统,我已经编写了一个 check_availability 函数,该函数将根据客户要求的类别检查房间是否为空,但事实证明该函数忽略了数据库,因为它只关心现有的一个不是添加的一个。这是代码:


从 django.db 导入模型


from django.contrib.auth.models import User

class 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.name


class 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 render

from .forms import BookingForm

from .models import Booking, RoomCategory, Room

from django.views.generic import FormView  

from Hotel.Availabililty.Available import check_availability

from django.http import HttpResponse

# Create your views here.


class BookingFormview(FormView):

    form_class      = BookingForm

    template_name   = 'Hotel/bookingformview.html'

                )



守着星空守着你
浏览 120回答 1
1回答

心有法竹

Room您可以获得不存在重叠的 s列表Booking:roomlist = Room.objects.filter(&nbsp; &nbsp; room_category__name=data['room']).exclude(&nbsp; &nbsp; booking__check_in__lte=data['check_out'],&nbsp; &nbsp; 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则两个范围合计。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python