Django查询:Count和Group BY [英] Django query: Count and Group BY

查看:513
本文介绍了Django查询:Count和Group BY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,我试图想出django的方式:
我想从Call调用最后100个呼叫。这很容易:
call = Call.objects.all()。order_by(' - call_time')[:100]



然而下一部分我可以找不到通过django的ORM做的方式。我想获得一个call_types的列表和每个人在之前的queryset我刚刚做的呼叫数。通常我会这样查询:SELECT COUNT(id),calltype FROM call WHERE id IN(SELECT id FROM call ORDER BY call_time DESC LIMIT 100)GROUP BY calltype;



/ p>

这是我的2个模型:

  class Call(models.Model):
call_time = models.DateTimeField(Call Time,auto_now = False,auto_now_add = False)
description = models.CharField(max_length = 150)
response = models.CharField(max_length = 50)
event_num = models.CharField(max_length = 20)
report_num = models.CharField(max_length = 20)
address = models.CharField(max_length = 150)
zip_code = models.CharField(max_length = 10)
geom = models.PointField(srid = 4326)
calltype = models.ForeignKey(CallType)

objects = models.GeoManager()

class CallType(models.Model):
name = models.CharFi eld(max_length = 50)
description = models.CharField(max_length = 150)
active = models.BooleanField()
time_init = models.DateTimeField(Date Added,auto_now = False, auto_now_add = True)

objects = models.Manager()


解决方案

尝试这样:

  calls = Call.objects.all()。order_by(' -  call_time ')[:100] 
types = CallType.objects.filter(call__in = calls).annotate(Count('call'))

您可能需要向后一个查询器添加distinct()。


I have a query that I'm trying to figure the "django" way of doing it: I want to take the last 100 calls from Call. Which is easy: calls = Call.objects.all().order_by('-call_time')[:100]

However the next part I can't find the way to do it via django's ORM. I want to get a list of the call_types and the number of calls each one has WITHIN that previous queryset i just did. Normally i would do a query like this: "SELECT COUNT(id),calltype FROM call WHERE id IN ( SELECT id FROM call ORDER BY call_time DESC LIMIT 100 ) GROUP BY calltype;"

I can't seem to find the django way of doing this particular query.

Here are my 2 models:

class Call( models.Model ):
    call_time = models.DateTimeField( "Call Time", auto_now = False, auto_now_add = False )
    description = models.CharField( max_length = 150 )
    response = models.CharField( max_length = 50 )
    event_num = models.CharField( max_length = 20 )
    report_num = models.CharField( max_length = 20 )
    address = models.CharField( max_length = 150 )
    zip_code = models.CharField( max_length = 10 )
    geom = models.PointField(srid=4326)
    calltype = models.ForeignKey(CallType)

    objects = models.GeoManager()

class CallType( models.Model ):
    name = models.CharField( max_length = 50 )
    description = models.CharField( max_length = 150 )
    active = models.BooleanField()
    time_init = models.DateTimeField( "Date Added", auto_now = False, auto_now_add = True )

    objects = models.Manager()

解决方案

try this:

calls = Call.objects.all().order_by('-call_time')[:100]
types = CallType.objects.filter(call__in=calls).annotate(Count('call'))

you might need to add distinct() to the latter queryset though.

这篇关于Django查询:Count和Group BY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆