本文是小编为大家收集整理的关于Django查询。计数和Group BY的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我有一个查询,我正在尝试弄清" django"的方法: 我想接听最后100个电话.这很容易: 呼叫= call.objects.all().order_by(' - call_time')[:100]
但是,下一部分我找不到通过Django的ORM找到它的方法.我想获取一个call_types的列表,以及每个呼叫的呼叫数量在我刚刚做过的以前的QuerySet中.通常,我会进行这样的查询:"选择计数(id),calltype从call where in where(通过call_time disc limit限制从呼叫订单中选择ID ID)组成calltype;"
我似乎找不到执行此特定查询的Django的方式.
这是我的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.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()
推荐答案
尝试以下方法:
calls = Call.objects.all().order_by('-call_time')[:100] types = CallType.objects.filter(call__in=calls).annotate(Count('call'))
您可能需要在后一个QuerySet中添加不同的().
其他推荐答案
使用Django 1.1.1这个令人惊讶的是;
from django.db.models import Count calls = Call.objects.all().order_by('-call_time')[:100] groups = calls.values('calltype').annotate(Count('calltype'))
它有效,但在整个对象上返回,不仅是所选的100个!无法理解.
但是,这种解决方法可以帮助(今天由Calltype分组的摘要):
import datetime Call.objects.order_by('-call_time').filter(call_time__gte=datetime.datetime.today() - datetime.timedelta(days=1)).values('calltype').annotate(Count('calltype'))
问题描述
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.
其他推荐答案
With django 1.1.1 this surprisingly doesnt work ;
from django.db.models import Count calls = Call.objects.all().order_by('-call_time')[:100] groups = calls.values('calltype').annotate(Count('calltype'))
it works but returns on the whole objects, not only the 100 selected! cant understand.
but this workaround could help (today calls summary grouped by calltype) :
import datetime Call.objects.order_by('-call_time').filter(call_time__gte=datetime.datetime.today() - datetime.timedelta(days=1)).values('calltype').annotate(Count('calltype'))