【发布时间】:2015-06-07 21:00:30
【问题描述】:
我试图在使用注释时获取选项的显示名称,但我无法弄清楚。我有以下查询:
Survey.objects.values('what').annotate(count=Count('why')).order_by()
结果是:
[{'count': 34, 'what': u'a'},
{'count': 39, 'what': u'c'},
{'count': 40, 'wat': u'p'}]
但我想要显示选择字段的名称而不是键的东西:
[{'count': 34, 'what': u'appreciative'},
{'count': 39, 'what': u'creative'},
{'count': 40, 'wat': u'promising'}]
我尝试了 get_what_display (如文档和有关此主题的其他 stackoverflow 答案中所述),但 django 抛出错误。即以下似乎不起作用
Survey.objects.values('get_what_display').annotate(count=Count('why')).order_by()
【问题讨论】:
-
get_prop_display是属性而不是数据库字段。所以你必须循环它并更新显示值AFAIK。
标签: python django django-models django-forms django-queryset