从 Django 中检索到的模型对象访问属性
Access attributes from retrieved model object in django
我正在尝试访问传递给 class 的模型对象的属性。所以我的观点是:
my_events = Event.objects.order_by('start').filter(start__year=year, start__month=month)
然后我将其传递给我的 class,如下所示:
calendar = EventCalendar(my_events).formatmonth(year, month)
然后这是我的 class
# -*- coding: UTF-8 -*-
from calendar import HTMLCalendar
from datetime import date
from itertools import groupby
from django.utils.html import conditional_escape as esc
class EventCalendar(HTMLCalendar):
def __init__(self, event):
super(EventCalendar, self).__init__()
self.event = self.group_by_day(event)
def formatday(self, day, weekday):
if day != 0:
cssclass = self.cssclasses[weekday]
if date.today() == date(self.year, self.month, day):
cssclass += ' today'
if day in self.event:
cssclass += ' filled'
extra_div = ['<div class="event_container"><ul>']
for event in self.event[day]:
extra_div.append('<li>')
extra_div.append('<a href="%s">' % event.get_absolute_url())
extra_div.append(esc(event.title))
extra_div.append('</a></li>')
extra_div.append('</ul></div>') #END event_container
#Custom div wrapper
extra_div.append('<div class="extra_container">')
#Custom div add_event
extra_div.append('<div class="add_event">')
extra_div.append('<a href="#">add event</a>')
extra_div.append('</div>')
#Custom div show_day
extra_div.append('<div class="show_day">')
extra_div.append('<a href="#">show day</a>')
extra_div.append('</div>')
extra_div.append('</div>') #END extra_container
return self.day_cell(cssclass, '%d %s' % (day, ''.join(extra_div)))
#Custom div wrapper
extra_div = ['<div class="extra_container">']
#Custom div add_event
extra_div.append('<div class="add_event">')
extra_div.append('<a href="#">add event</a>')
extra_div.append('</div>')
#Custom div show_day
extra_div.append('<div class="show_day">')
extra_div.append('<a href="#">show day</a>')
extra_div.append('</div>')
extra_div.append('</div>')
return self.day_cell(cssclass, '%d %s' % (day, ''.join(extra_div)))
return self.day_cell('noday', ' ')
def formatmonth(self, year, month):
self.year, self.month = year, month
return super(EventCalendar, self).formatmonth(year, month)
def group_by_day(self, event):
event_dict = {}
start = event.start.day
end = event.end.day
return event_dict
def day_cell(self, cssclass, body):
return '<td class="%s">%s</td>' % (cssclass, body)
问题出在代码中的 group_by_day(self, event)
部分。我正在尝试检索活动的开始和结束日期。与:
start = event.start.day
end = event.end.day
但是当我这样做时,我得到了错误
'QuerySet' object has no attribute 'start'
这是否意味着当我检索对象时我只获得了对它的引用,类似于惰性实例化?还是我只是试图访问错误?
抱歉,如果问题很简单,python/django 真的很新。
谢谢
Event.objects.order_by('start').filter(start__year=year, start__month=month)
Returns 匹配 Event
对象的列表。然后将此 QuerySet 列表传递给:
group_by_day(self, event)
从您的参数名称来看,您认为您传递的是单个事件。所以没用。
我正在尝试访问传递给 class 的模型对象的属性。所以我的观点是:
my_events = Event.objects.order_by('start').filter(start__year=year, start__month=month)
然后我将其传递给我的 class,如下所示:
calendar = EventCalendar(my_events).formatmonth(year, month)
然后这是我的 class
# -*- coding: UTF-8 -*-
from calendar import HTMLCalendar
from datetime import date
from itertools import groupby
from django.utils.html import conditional_escape as esc
class EventCalendar(HTMLCalendar):
def __init__(self, event):
super(EventCalendar, self).__init__()
self.event = self.group_by_day(event)
def formatday(self, day, weekday):
if day != 0:
cssclass = self.cssclasses[weekday]
if date.today() == date(self.year, self.month, day):
cssclass += ' today'
if day in self.event:
cssclass += ' filled'
extra_div = ['<div class="event_container"><ul>']
for event in self.event[day]:
extra_div.append('<li>')
extra_div.append('<a href="%s">' % event.get_absolute_url())
extra_div.append(esc(event.title))
extra_div.append('</a></li>')
extra_div.append('</ul></div>') #END event_container
#Custom div wrapper
extra_div.append('<div class="extra_container">')
#Custom div add_event
extra_div.append('<div class="add_event">')
extra_div.append('<a href="#">add event</a>')
extra_div.append('</div>')
#Custom div show_day
extra_div.append('<div class="show_day">')
extra_div.append('<a href="#">show day</a>')
extra_div.append('</div>')
extra_div.append('</div>') #END extra_container
return self.day_cell(cssclass, '%d %s' % (day, ''.join(extra_div)))
#Custom div wrapper
extra_div = ['<div class="extra_container">']
#Custom div add_event
extra_div.append('<div class="add_event">')
extra_div.append('<a href="#">add event</a>')
extra_div.append('</div>')
#Custom div show_day
extra_div.append('<div class="show_day">')
extra_div.append('<a href="#">show day</a>')
extra_div.append('</div>')
extra_div.append('</div>')
return self.day_cell(cssclass, '%d %s' % (day, ''.join(extra_div)))
return self.day_cell('noday', ' ')
def formatmonth(self, year, month):
self.year, self.month = year, month
return super(EventCalendar, self).formatmonth(year, month)
def group_by_day(self, event):
event_dict = {}
start = event.start.day
end = event.end.day
return event_dict
def day_cell(self, cssclass, body):
return '<td class="%s">%s</td>' % (cssclass, body)
问题出在代码中的 group_by_day(self, event)
部分。我正在尝试检索活动的开始和结束日期。与:
start = event.start.day
end = event.end.day
但是当我这样做时,我得到了错误
'QuerySet' object has no attribute 'start'
这是否意味着当我检索对象时我只获得了对它的引用,类似于惰性实例化?还是我只是试图访问错误?
抱歉,如果问题很简单,python/django 真的很新。 谢谢
Event.objects.order_by('start').filter(start__year=year, start__month=month)
Returns 匹配 Event
对象的列表。然后将此 QuerySet 列表传递给:
group_by_day(self, event)
从您的参数名称来看,您认为您传递的是单个事件。所以没用。