Django - 尝试在 views.py 中迭代模型 class (db table) 时出错(需要比较日期)?
Django - Error while trying to iterate a model class (db table) in views.py (need to compare dates)?
我们的想法是比较存储的检查日期和结帐日期,以及从 html 文件(酒店预订,我试图查看是否有 habitaciones [房间]可用)
**** views.py 中的代码 *****
def 储备(请求):
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()
#code In models.py
class 居住地(models.Model):
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')
错误信息:
请求方法:POST
请求 URL:http://127.0.0.1:8000/reservas.html/
Django 版本:3.1.1
异常类型:TypeError
异常值:'ModelBase' 对象不可订阅
异常位置:/home/yamil/adaw2/hotel-Django/hotel/hotel/hotelApp1/views.py, line 94, in reservas
Python 可执行文件:/usr/bin/python3
Python版本:3.8.5
- if Habitacion[h].fecha_checkout <= pickerL_checking 且 pickerR_checkout <= Habitacion[h].fecha_checking:
提前致谢!!!
问题可能出在您迭代 habitaciones
的方式上。
您查询:
habitaciones = Habitacion.objects.all()
但是,当您遍历该列表时,您将对象放入对象本身(Habitacion 不是列表,它是一个模型):
if Habitacion[h].fecha_checkout >= pickerL_checking...
尝试像这样替换它:
if h.fecha_checkout >= pickerL_checking and pickerR_checkout <= h.fecha_checking:
如果有帮助请告诉我。
我们的想法是比较存储的检查日期和结帐日期,以及从 html 文件(酒店预订,我试图查看是否有 habitaciones [房间]可用)
**** views.py 中的代码 *****
def 储备(请求):
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()
#code In models.py
class 居住地(models.Model):
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')
错误信息:
请求方法:POST 请求 URL:http://127.0.0.1:8000/reservas.html/ Django 版本:3.1.1 异常类型:TypeError
异常值:'ModelBase' 对象不可订阅
异常位置:/home/yamil/adaw2/hotel-Django/hotel/hotel/hotelApp1/views.py, line 94, in reservas Python 可执行文件:/usr/bin/python3 Python版本:3.8.5
- if Habitacion[h].fecha_checkout <= pickerL_checking 且 pickerR_checkout <= Habitacion[h].fecha_checking:
提前致谢!!!
问题可能出在您迭代 habitaciones
的方式上。
您查询:
habitaciones = Habitacion.objects.all()
但是,当您遍历该列表时,您将对象放入对象本身(Habitacion 不是列表,它是一个模型):
if Habitacion[h].fecha_checkout >= pickerL_checking...
尝试像这样替换它:
if h.fecha_checkout >= pickerL_checking and pickerR_checkout <= h.fecha_checking:
如果有帮助请告诉我。