给出一个表
Table
col_1 col_2 col_3 ...
1 2 4
4 2 4
4 1 2
我可以用
计算单个列Table.objects.filter(key=int).values('col_1').annotate(Count('col_1'))
并获得该列的预期结果:
[{col_1: 4, col_1__count: 2}, {col_1: 1, col_1__count: 1}]
如何有效地(一个数据库往返)为多个列执行此操作以实现以下输出?
[{col_1: 4, col_1__count: 2}, {col_1: 1, col_1__count: 1},
{col_2: 2, col_2__count: 2}, {col_2: 1, col_2__count: 1},
{col_3: 4, col_3__count: 2}, {col_3: 2, col_3__count: 1},
... ]