【问题标题】:How to Append the values of ValueQuryset to array?如何将值查询集的值附加到数组?
【发布时间】:2014-03-07 07:27:01
【问题描述】:

我的视图.py:

from django.db.models import Count
def test1(request):
    states = Loksabha.objects.values('state').distinct('state')
    terms = Loksabha.objects.values('term').distinct('term')
    dataset = Loksabha.objects.all()
    state_filter=Loksabha.objects.filter(state='Maharashtra',term='Fourteenth Lok Sabha(2004-  09)').annotate(num=Count('party',distinct=True))
    age_filter=state_filter.values('party').annotate(Count('party'))
    xdata=[]
    ydata=[]
    for b in state_filter:
        xdata.append(b.party)
        ydata.append(b.num)
    chartdata = {'x': xdata, 'y': ydata}
    charttype = "pieChart"
    chartcontainer = 'piechart_container'

我使用 django-nvd3 来显示图表,我的 state_filter 查询答案是 coreect,但我无法理解将 ValueQueryset 的值传递给 xdata[]ydata[]。我的 state_filter 查询集值传递给age_filter

age_filter 值为:

[{'party': 'Shiv Sena', 'party__count': 14},
 {'party': 'Indian Nationlist Congress', 'party__count': 15},
 {'party': 'Nationlist Congress Party', 'party__count': 9},
 {'party': 'Republican Party of India(A)', 'party__count': 1},
 {'party': 'Bharatiya Janata Party', 'party__count': 14},
 {'party': 'Independent', 'party__count': 1}]

【问题讨论】:

    标签: django


    【解决方案1】:

    ValueQuerySet 产生字典。通过索引获取项目,而不是访问属性。

    替换以下行:

    for b in state_filter:
        xdata.append(b.party)
        ydata.append(b.num)
    

    与:

    for d in age_filter:
        xdata.append(b['party'])
        ydata.append(b['party_count'])
    

    【讨论】:

    • 感谢假兄弟
    猜你喜欢
    • 2020-09-28
    • 2018-03-03
    • 2021-12-14
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2020-03-01
    相关资源
    最近更新 更多