在Django中,如何汇总一个注释结果与另一个注释结果?

时间:2020-04-01 14:16:14

标签: django django-queryset django-annotate

这是我一直在做的一项棘手的操作。我有以下内容:

users.values('first_name').annotate(demo=F('demo_responses__demographic__display'), response=F('demo_responses__option__display'))

运行此命令时,得到的结果如下:

[
 {'first_name': 'Bob', 'demo': 'Business Category', 'response': 'Engineering'}, 
 {'first_name': 'Bob', 'demo': 'Job Function', 'response': 'Training'}
 {'first_name': 'Joe', 'demo': 'Business Category', 'response': 'Restaurant'}, 
 {'first_name': 'Joe', 'demo': 'Job Function', 'response': 'Server'}
]

我想做的就是将它们压缩为以下内容:

[
 {'first_name': 'Bob', 'Business Category': 'Engineering', 'Job Function': 'Training'}, 
 {'first_name': 'Joe', 'Business Category': 'Restaurant', 'Job Function': 'Server'}, 
]

以这种格式保存它对于我生成的某些报告会有所帮助。我知道我可以通过编程方式执行此操作,但是我不知道是否可以使用Django查询魔术来更快地到达那里。有什么想法吗?

0 个答案:

没有答案