为什么django组被错误的领域?注释()

时间:2015-06-25 16:43:55

标签: python django python-2.7 model rdbms

为什么Django Group by id Field? 这是queryset:

MyModel.objects\
    .filter(
        status=1,
        created__gte=datestart,
        created__lte=dateend)\
    .values('terminal_id')\
    .annotate(
        total_pa=Sum('amount'),
        total_ar=Sum('amount_result')).query

但它按照pk字段排序和分组。

... GROUP BY paymentid ORDER BY paymentid ...

而不是

... GROUP BY paymentterminal_id ORDER BY paymentterminal_id ...

1 个答案:

答案 0 :(得分:4)

See the "Warning" here: https://docs.djangoproject.com/en/1.8/topics/db/aggregation/#interaction-with-default-ordering-or-order-by Perhaps the default ordering is getting in the way. Try adding .order_by() to the end of your query to see if that clears the ordering.