Default ordering affects aggregation queries like that: 
https://docs.djangoproject.com/en/stable/topics/db/aggregation/#interaction-with-default-ordering-or-order-by

On Wednesday, August 10, 2016 at 3:44:33 PM UTC-4, Neto wrote:
>
> I'm trying to get number of records per month, but Django does not return 
> correctly when there is "ordering" in class Meta Model.
>
> I have the Abc model, with 4 records made this month, look what happens 
> (Django 1.10, PostgreSQL):
>
> class Abc(Base):
>     
>     class Meta:
>         ordering = ['-date_pub']
>
>
> >>> Abc.objects.annotate(month=TruncMonth('created_at')).values('month').
> annotate(total=Count('id')).values('total')
> <QuerySet [{'total': 1}, {'total': 1}, {'total': 1}, {'total': 1}]>
>
> Without "ordering"
>
> class Abc(Base):
>     pass
>
>
> >>> Abc.objects.annotate(month=TruncMonth('created_at')).values('month').
> annotate(total=Count('id')).values('total')
> <QuerySet [{'total': 4}]>
>
> Why this happens? Is it a bug?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8055b5b9-f4ef-466e-87b7-dc5879d121df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to