Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.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模型中的属性_Python_Django_Properties - Fatal编程技术网

Python Django模型中的属性

Python Django模型中的属性,python,django,properties,Python,Django,Properties,我想使用模型中的属性改进django中的视图。 注释代码运行良好,但当我在模型中添加属性时,模板中出现了空白 views.py: cal_day = {} cal_day['day'] = day cal_day['day_event'] = False cal_day['sports'] = [] for event in event_list: if day >= event.res

我想使用模型中的属性改进django中的视图。 注释代码运行良好,但当我在模型中添加属性时,模板中出现了空白

views.py:

        cal_day = {}
        cal_day['day'] = day
        cal_day['day_event'] = False
        cal_day['sports'] = []
        for event in event_list:
            if day >= event.reservation_date.date() and day <= event.reservation_date.date():
                cal_day['day_event'] = True
                hours_week.append(event.reservation_date.time())
                # a = Reservation.objects.get(id=event.id)                    
                # d = dict()
                # d['id'] = a.id
                # d['sport'] = a.sport
                # d['trainer'] = a.trainer
                # d['reservation_date'] = a.reservation_date
                # d['free_space'] = (a.sport.capacity - a.users.count())
                # d['duration'] = a.sport.duration
                # d['time'] = a.reservation_date.time()
                # cal_day['sports'].append(d)
                print(Reservation.reservation_info)
                cal_day['sports'].append(Reservation.reservation_info)
在终端中获取打印对象:


如果有人能帮助我理解我做错了什么,以及我如何改进我的错误,我将非常感激:)

您不能使用该类访问该属性。注释代码正常工作,因为您正在使用一个实例:

a = Reservation.objects.get(id=event.id)
d = dict()
# ...
d['reservation_date'] = a.reservation_date  # this works!

一个问题,嗯?听起来很严重。很好的破解,因为标题上不允许使用“问题”这个词。您似乎不打印实例属性,而是打印类1。
a = Reservation.objects.get(id=event.id)
d = dict()
# ...
d['reservation_date'] = a.reservation_date  # this works!