0

首先,感谢您的时间和帮助:)

我在下面创建了模型,并且有一段时间一直在愉快地使用场景 1(场景也在下面)进行编码。

现在我开始使用图表,需要以场景 2 的形式访问数据。场景 2 有效——附加值是我有时间在等待结果的同时制作三明治。我知道这是由于我四次访问数据库这一事实造成的,所以我可能创建了代码历史上效率最低的一段代码,但我未能使其正常工作。

我非常感谢您帮助理解如何在不违反 Django 所代表的每一个原则的情况下达到预期的结果。

再次感谢你!

Model:

    class People_stats(models.Model):
    people_stats = models.CharField(max_length=50, primary_key=True)        
    con_basmat = models.IntegerField(null=True, max_length=6) 
    con_cgoods = models.IntegerField(null=True, max_length=6) 
    con_cserv = models.IntegerField(null=True, max_length=6)                 


   SCENARIO 1 - when iterating in template 

     Views:

        people = ['Name', 'Benchmark1', 'Benchmark2', 'Benchmark3']
        people_names = [form.cleaned_data[p] for p in people]            

        profiles =  [People_stats.objects.filter(name__in=people_names)] 

     Template: 

        {% for p in profiles %}
          {%for d in p %}              
            <ul>
              <li>{{d.con_basmat}}</li>
              <li>{{d.con_cgoods}}</li>
              <li>{{d.con_cserv}}</li>                        
            </ul> 
          {%endfor%} 
        {% endfor %}       


    #Scenario 2 - when I need to access model fields and create a json.dump  

      Views:

        N = form.cleaned_data['Name']
        B1 = form.cleaned_data['Benchmark1']
        B2 = form.cleaned_data['Benchmark2']
        B3 = form.cleaned_data['Benchmark3']

        dir_prof1 =  People_stats.objects.filter(name__exact=N)
        dir_prof2 =  People_stats.objects.filter(name__exact=B1)
        dir_prof3 =  People_stats.objects.filter(name__exact=B2)
        dir_prof4 =  People_stats.objects.filter(name__exact=B3)

        for d in dir_prof1:               
          con_ind1 = ['Basic Materials', d.con_basmat],['Consumer Goods', d.con_cgoods],['Consumer Services',d.con_cserv]

        for d in dir_prof2:               
          con_ind2 = ['Basic Materials', d.con_basmat],['Consumer Goods', d.con_cgoods],['Consumer Services',d.con_cserv]

        for d in dir_prof3:               
          con_ind3 = ['Basic Materials', d.con_basmat],['Consumer Goods', d.con_cgoods],['Consumer Services',d.con_cserv]

        for d in dir_prof4:               
          con_ind4 = ['Basic Materials', d.con_basmat],['Consumer Goods', d.con_cgoods],['Consumer Services',d.con_cserv]

        con_ind_dmp1 = json.dumps(con_ind1, cls=DjangoJSONEncoder)

        con_ind_dmp2 = json.dumps(con_ind2, cls=DjangoJSONEncoder)

        con_ind_dmp3 = json.dumps(con_ind3, cls=DjangoJSONEncoder)

        con_ind_dmp4 = json.dumps(con_ind4, cls=DjangoJSONEncoder)

      Template: 
         I don't think we need to make it that far to realize that this is not the way to go :( 
4

1 回答 1

0

尝试这个

对于 dir_prof1 中的 d:
con_ind1 = {'基本材料':d.con_basmat,'消费品':d.con_cgoods,'消费者服务':d.con_cserv}

对于 dir_prof2 中的 d:
con_ind2 = {'基本材料':d.con_basmat,'消费品':d.con_cgoods,'消费者服务',d.con_cserv}

对于 dir_prof3 中的 d:
con_ind3 = {'基本材料':d.con_basmat,'消费品':d.con_cgoods,'消费者服务':d.con_cserv}

对于 dir_prof4 中的 d:
con_ind4 = {'基本材料':d.con_basmat,'消费品':d.con_cgoods,'消费者服务':d.con_cserv}

con_ind_dmp1 = json.dumps(con_ind1)

con_ind_dmp2 = json.dumps(con_ind2)

con_ind_dmp3 = json.dumps(con_ind3)

con_ind_dmp4 = json.dumps(con_ind4)

于 2013-04-19T10:28:26.167 回答