我的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查询集值传递给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}]
答案 0 :(得分:3)
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'])