Python Django-尝试在views.py中迭代模型类(db表)时出错(需要比较日期)?
我们的想法是将存储的结账日期和结账日期与从html文件中获取的结账日期进行比较(酒店预订,我想看看是否有可用的住宿[房间]) ****views.py中的代码***** def预订(请求): 错误消息: 申请方式:邮寄 请求URL: Django版本:3.1.1 异常类型:TypeError 异常值:'ModelBase'对象不可下标 异常位置:/home/yamil/adaw2/hotel Django/hotel/hotelApp1/views.py,第94行,在reservas中 Python可执行文件:/usr/bin/python3 Python版本:3.8.5Python Django-尝试在views.py中迭代模型类(db表)时出错(需要比较日期)?,python,django,model,Python,Django,Model,我们的想法是将存储的结账日期和结账日期与从html文件中获取的结账日期进行比较(酒店预订,我想看看是否有可用的住宿[房间]) ****views.py中的代码***** def预订(请求): 错误消息: 申请方式:邮寄 请求URL: Django版本:3.1.1 异常类型:TypeError 异常值:'ModelBase'对象不可下标 异常位置:/home/yamil/adaw2/hotel Django/hotel/hotelApp1/views.py,第94行,在reservas中 Pyth
habitaciones
上的迭代方式
您可以查询:
habitaciones=Habitacion.objects.all()
但是,当您迭代该列表时,您将对象放入对象本身(Habitacion不是列表,它是一个模型):
如果Habitacion[h].fecha\u checkout>=pickerL\u checking
试着像这样替换它:
如果h.fecha_checkout>=pickerL_checking和pickerR_checkout,则应将代码和错误作为文本添加到帖子正文中,而不是图片。
pickerR = request.POST.get('pickerR', None)
pickerR_checkout = datetime.datetime.strptime(pickerR, '%d/%m/%Y') #convierto la fecha de tipo string a datetime
pickerL = request.POST.get('pickerL', None)
pickerL_checking = datetime.datetime.strptime(pickerL, '%d/%m/%Y') #convierto la fecha de tipo string a datetime
habitaciones = Habitacion.objects.all()
for hd in habitaciones:
if Habitacion[hd].fecha_checkout >= pickerL_checking and pickerR_checkout <= Habitacion[hd].fecha_checking:
cantHabitaciones = `enter code here`int(request.POST.get('cant_habitaciones'))
cantH = (request.POST.get('cant_huespedes'))
tipo_habitaciones = models.tipoHabitacion.objects.all()
precio = models.IntegerField()
numero = models.IntegerField()
disponible = models.BooleanField()
tipo = models.ForeignKey(tipoHabitacion, on_delete=models.CASCADE)
fecha_checking = models.DateField('%d/%m/%Y')
fecha_checkout = models.DateField('%d/%m/%Y')