Unfortunately, I don't think mentioning the issue every place where someone 
might be doing an aggregation query is practical. I did recently add a link 
about it to the Meta.ordering docs recently [0], so hopefully that helps 
raise awareness.

[0] 
https://github.com/django/django/commit/3aaf6cf0f3288986c4ce56defea26420f8a48534

On Wednesday, August 10, 2016 at 4:41:29 PM UTC-4, Neto wrote:
>
> Thanks! Should have a note about it on the page "database-funcions":
> https://docs.djangoproject.com/en/1.10/ref/models/database-functions/#trunc
>
> Em quarta-feira, 10 de agosto de 2016 17:17:31 UTC-3, Tim Graham escreveu:
>>
>> 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/74ed437d-65f3-4144-8c1d-a4c0dd767ccf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to