Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-尝试在views.py中迭代模型类(db表)时出错(需要比较日期)?_Python_Django_Model - Fatal编程技术网

Python Django-尝试在views.py中迭代模型类(db表)时出错(需要比较日期)?

Python 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

我们的想法是将存储的结账日期和结账日期与从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.5


  • 如果Habitacion[h].fecha_checkout则问题可能在于您在
    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')