I want to book a specific time for the user (start hour and end hour ) and no one else can book at the same time Example Like : John made an appointment on the 5th day of the month at 7 to 8 o'clock i want no one else can booking at the same time and day
我想为用户预订一个特定的时间(开始时间和结束时间),其他人不能同时预订例如:John在本月5日7点到8点预约,我希望其他人不能在同一时间和同一天预订
and i want to check the Period because if someone like john booking start time 5pm to 8pm
no one can booking like stat 6pm to 7 pm at the same day
我想查看Period,因为如果像john这样的人预订了下午5点到8点的开始时间,没有人可以在同一天下午6点到7点这样预订
How I can Do This in Models Django
如何在模型Django中做到这一点
I could not find a solution to this problem
我找不到解决这个问题的办法
enter image description here
在此处输入图像描述
im using timing to know how long the booking take
and i dont know what can i should do
我利用时间来知道预订需要多长时间,我不知道该怎么办
enter the link tow show the image of code
输入链接以显示代码的图像
更多回答
Please provide enough code so others can better understand or reproduce the problem.
请提供足够的代码,以便其他人能够更好地理解或重现问题。
优秀答案推荐
You can override the model's clean
method.
可以覆盖模型的clean方法。
from django.db.models import Q
from django.core.exceptions import ValidationError
class OpeningHour(models.Model):
made_on = models.DateField()
from_hour = models.TimeField()
to_hour = models.TimeField()
def __str__(self):
return f"{self.from_hour} to {self.to_hour}"
def clean(self):
#Ensure that from_hour is not greater than to_hour
if self.from_hour > self.to_hour:
raise ValidationError("Start time can not be greater than end time")
in_range = (self.from_hour, self.to_hour)
filter = OpeningHour.objects.filter(made_on=self.made_on).filter(Q(from_hour__range=in_range) | Q(to_hour__range=in_range)).exists()
if filter:
raise ValidationError(f"The booked time ({self.from_hour} to {self.to_hour}) is occupied.")
return super().clean()
更多回答
我是一名优秀的程序员,十分优秀!