简体   繁体   English

Django按多个字段分组

[英]Django group by multiple fields

I want to transform this SQL request into a Django view:我想将此 SQL 请求转换为 Django 视图:

SELECT color, shape, eyes, count(1) FROM animals WHERE master_id = 1 GROUP BY color, shape, eyes 

In order to do that I've tried:为了做到这一点,我尝试过:

animals = Animals.objects.filter(id=1).values('color', 'shape', 'eyes').annotate(ct=Count('color'), dg=Count('shape'), br=Count('eyes'))

And then loop on the result to find the count for each one (not very optimized) and the result isn't good.然后循环结果以找到每个(不是很优化)的计数,结果不好。 That's group by as I want but doesn't care about the id.这是我想要的分组,但不关心 id。

EDIT:编辑:

<QuerySet [
    { 'color': brown, 'shape': blabla, 'eyes': toto, 'ct': 2 },
    { 'color': black, 'shape': blabla, 'eyes': toto, 'ct': 1 },
    { 'color': yellow, 'shape': xxxxx, 'eyes': ok, 'ct': 4 }
]>

SECOND EDIT:第二次编辑:

If I try that:如果我尝试:

Animals.objects.filter(
    master_id=1
).values('color', 'shape', 'eyes').annotate(
    ct=Count('id')
).order_by('color', 'shape', 'eyes')

I have this result without count:我有这个结果,数不胜数:

<QuerySet [
    { 'color': brown, 'shape': blabla, 'eyes': toto},
    { 'color': black, 'shape': blabla, 'eyes': toto},
    { 'color': yellow, 'shape': xxxxx, 'eyes': ok }
]>

LAST EDIT:最后编辑:

The Animal table doesn't have count or ct column but I need it in my result Animal 表没有 count 或 ct 列,但我的结果中需要它

A complete equivalent query would be:一个完整的等效查询将是:

Animals.objects.filter(
    master_id=1
).values('color', 'shape', 'eyes').annotate(
    ct=Count('id')
).order_by('color', 'shape', 'eyes')

This will then result in a QuerySet that wraps dictionaries, for example:这将产生一个包装字典的QuerySet ,例如:

<QuerySet [
    { 'color': 0, 'shape': 2, 'eyes': 1, 'ct': 1 },
    { 'color': 0, 'shape': 3, 'eyes': 3, 'ct': 4 },
    { 'color': 1, 'shape': 0, 'eyes': 0, 'ct': 2 },
    { 'color': 2, 'shape': 2, 'eyes': 1, 'ct': 5 },
]>

For example:例如:

>>> Animals.objects.create(color='foo', shape='bar', eyes='brown', master_id=1)
<Animal: Animal object (2)>
>>> Animals.objects.create(color='foo', shape='bar', eyes='brown', master_id=1)
<Animal: Animal object (3)>
>>> Animals.objects.create(color='foo', shape='bar', eyes='blue', master_id=1)
<Animal: Animal object (4)>
>>> Animals.objects.create(color='foo', shape='rectangle', eyes='brown', master_id=1)
<Animal: Animal object (5)>
>>> Animals.objects.create(color='red', shape='rectangle', eyes='brown', master_id=1)
<Animal: Animal object (6)>
>>> Animals.objects.filter(
...     master_id=1
... ).values('color', 'shape', 'eyes').annotate(
...     ct=Count('id')
... ).order_by('color', 'shape', 'eyes')
<QuerySet [{'color': 'foo', 'shape': 'bar', 'eyes': 'blue', 'ct': 1}, {'color': 'foo', 'shape': 'bar', 'eyes': 'brown', 'ct': 2}, {'color': 'foo', 'shape': 'rectangle', 'eyes': 'brown', 'ct': 1}, {'color': 'red', 'shape': 'rectangle', 'eyes': 'brown', 'ct': 1}]> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM