Python Django模型中的属性
我想使用模型中的属性改进django中的视图。 注释代码运行良好,但当我在模型中添加属性时,模板中出现了空白 views.py: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
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!