我需要比较一下预订日期和时间, 在没有人预订房间的情况下预订房间
当我试图比较两个日期时,我得到了下面的错误 无法比较初始偏移量和感知偏移量的日期时间
这是我的检查可用性功能
import datetime
from booking.models import Booking
from mainapp.models import Room
def check_avaliblity(room,Check_in,Check_out):
avaliblity_list=[] #It will return binch of True and False
booking_list=Booking.objects.filter(room=room)#It will check bookings of specific room ex:101
for booking in booking_list:
if booking.Check_in>Check_out or booking.Check_out<Check_in:
#booking.check_in and booking.check_out is existing booking
avaliblity_list.append(True)
else:
avaliblity_list.append(False) #If any of list in avaliblity_list is get False The Booking cannot happend
return all(avaliblity_list)
这是我的预订模式
class Booking(models.Model):
"""Django data model Booking"""
user=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
room=models.ForeignKey(Room,on_delete=models.CASCADE)
Check_in = models.DateTimeField(datetime.datetime.strptime(str(datetime.datetime.now()),'%Y-%m-%d %H:%M:%S.%f').strftime("%Y-%m-%d %H:%M"))
Check_out = models.DateTimeField(datetime.datetime.strptime(str(datetime.datetime.now()),'%Y-%m-%d %H:%M:%S.%f').strftime("%Y-%m-%d %H:%M"))
class Meta:
verbose_name = 'Booking'
verbose_name_plural = 'Bookings'
def __str__(self):
return f'{self.user} booked {self.room} from {self.Check_in} to {self.Check_out}'
最后 Views.py
if request.method=='POST':
#This is from booking page
get_roomType=Room_Type.objects.get(roomtype=request.POST['type'])
roomid=get_roomType.id
getout=request.POST['check_out']
check_out=datetime.datetime.strptime(getout,'%m/%d/%Y %H:%M %p').strftime('%Y-%m-%d %H:%M:%S+00:00')
getin=request.POST['check_in']
check_in=datetime.datetime.strptime(getin,'%m/%d/%Y %H:%M %p').strftime('%Y-%m-%d %H:%M:%S+00:00')
check_in=datetime.datetime.strptime(check_in,'%Y-%m-%d %H:%M:%S+00:00')
check_out=(datetime.datetime.strptime(check_out,'%Y-%m-%d %H:%M:%S+00:00'))
print(type(check_out))
#This can set the values id to roomtype id
room_list=Room.objects.filter(room_type_id=get_roomType)
user_book=request.user
avalible_rooms=[]
for room in room_list:
if avalablity.check_avaliblity(room,check_in,check_out):
avalible_rooms.append(room)
if len(avalible_rooms)>0:
room=avalible_rooms
book_room=Booking.objects.create(
user=user_book,
room=room,
Check_in=check_in,
Check_out=check_out
)
book_room.save()
return HttpResponse(book_room)
else:
return HttpResponse("Not found")
type_of_room=Room_Type.objects.get(roomtype=room)
context={'type':type_of_room}
return render(request,'app/book.html',context)
这是我的错误
TypeError at /booking/booking/Lux
can't compare offset-naive and offset-aware datetimes
Request Method: POST
Request URL: http://127.0.0.1:8000/booking/booking/Lux
Django Version: 3.1.4
Exception Type: TypeError
Exception Value:
can't compare offset-naive and offset-aware datetimes
Exception Location: /home/hussain/Documents/GitHub/Noris-Hotel-Booking/Hotel/booking/bookingFunction/avalablity.py, line 9, in check_avaliblity
Python Executable: /home/hussain/Documents/GitHub/Noris-Hotel-Booking/env/bin/python3
Python Version: 3.8.6
Python Path:
['/home/hussain/Documents/GitHub/Noris-Hotel-Booking/Hotel',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/hussain/Documents/GitHub/Noris-Hotel-Booking/env/lib/python3.8/site-packages']
Server time: Sat, 12 Dec 2020 05:45:52 +0000
我试图上传截图,但我不知道如何上传图片,所以我复制了文本并粘贴到代码块中
这是我的解释 我在等你们的回复
您需要确保在数据库中,datetime是简单的或具有时区(如果使用PostgreSQL,则为timestamptz)
==============================
例如:
如果传递tzinfo,则在调用datetime.now()时,可以比较日期
相关问题 更多 >
编程相关推荐