Hi list,
I want to preface this by saying I’m really glad to see the ORM where it is in 1.8, it’s gotten really far and I now think it’s not hopeless to imagine writing complex things in the ORM…
So earlier this week I wrote a bit of a rant in the bug tracker about how annotate is confusing (https://code.djangoproject.com/ticket/25834).
A short summary is that annotate and values (with a spattering of order_by) are basically what decides grouping, but there’s a lot of trial and error involved in getting the right grouping with a tricky interaction between the two.
A behaviour that exemplifies this is that queryset.annotate(foo=thing).annotate(bar=other_thing) is not the same as queryset.annotate(foo=thing, bar=other_thing) given certain things. This goes against the intuitive interpretation of the queryset API IMO.
I think there should be some update to the API to render grouping more explicit. Absent that, given that the docs are
now oriented in a sort of “You shouldn’t need extras anymore” fashion, there really should be a “SQL to ORM” migration guide/cookbook to point out explicitly how to go from SELECT stuff FROM table GROUP BY properties to the right annotate/values. I found what jarshwah pointed out in the ticket was really helpful.
Also, some random feature requests:
.values(‘stuff’, my_thing=Coalesce(‘thing’, ‘stuff’)) should workYear and Month functions to extract years/months from date fieldsAnyways I wanted to share this experience so that anyone who has the courage to right new docs/continue evolving things can know of at least one team’s difficulties.
Raphael
Also, some random feature requests:
.values(‘stuff’, my_thing=Coalesce(‘thing’, ‘stuff’))should work
- there should be a provided
YearandMonthfunctions to extract years/months from date fields