Django模型属性与字典数据

时间:2014-02-09 05:56:51

标签: django django-models django-templates

我正在尝试访问包含我的模板中的字典的属性/计算字段我不太清楚我应该如何循环这些值,从代码的角度来看这是正确的方法。 ..正如您在下面看到的,我的视图创建了一个过滤器查询集,我希望该查询集中的每一行都包含来自模型的计算字段/属性的dict。任何建议将不胜感激。我很难确定在模板本身中循环数据的正确方法是什么。最终产品看起来像这样...

<div id={{provider.id}}>{{provider.name}}</div>
  <div class="dayofweek">Monday</div>
    <div class="timesavailable">
       <ul>
         <li>8:00</li>
         <li>8:30></li>
     </div>

型号:

from project.lib.file import GetSchedule
class Provider(models.Model):
....
@property
def current_schedule(self):
  sched = GetSchedule(self.id)
  return sched.returnSchedule()

助手功能:

class GetSchedule(object):
    schedule = {}
    def __init__(self, provider_id):
        self.provider = provider_id
        try:
            self.schedule = GetSchedule.__buildSchedule(self,self.provider)
        except ObjectDoesNotExist:
            print "Init failed - Cannot find provider in db with id:  %s" % self.provider
            raise ValueError

    def __buildSchedule(self, provider):
        #default values for now - dict will contain data soon
        pid = self.provider
        sched = {'Monday':{'8:00','8:30','10:00'}, 'Tuesday':{'10:00','10:30','11:00'}, 'Wednesday':{'9:30','10:00','10:30'}}
        val = {
            'Schedule' : sched
        }
        return val
    def returnSchedule(self):
        return self.schedule

查看:

prov_list = Provider.objects.filter(docspeciality__name=dft_spec)
context_dict = {
            'DftSpec': dft_spec,
            'DftZip': dft_zip,
            'DftIns': dft_ins,
            'DocList': prov_list}
return render_to_response('search/provider.html', context_dict, context_instance=RequestContext(request))

0 个答案:

没有答案