我的View.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 queryset值传递给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}]

最佳答案

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'])

10-05 22:09