Is there any particular reason why it's assumed you won't be performing a Max on a CharField?

20 views
Skip to first unread message

Joshua Russo

unread,
Sep 22, 2011, 8:16:11 PM9/22/11
to django-d...@googlegroups.com
I posted this in the users group but didn't get any response, so I'm bringing it here. 

Is there any particular reason why it's assumed you won't be performing a Max on a CharField? 

I tried to create the following QuerySet I get an error out of convert_values() of BaseDatabaseOperations in django/db/backends/__init__.py:

Document.objects.filter(documentType=1, event__eventType=3).annotate(event_date=Max('event__start_date'), subcommittee=Max('event__subcommittee__name')).order_by('subcommittee', 'event_date')

The reason why I performed a Max on the subcommittee name is that the relation between Document and Event is actually a many to many, but I enforce this particular documentType to only once per Event. 

The fix is easy. Add any character based field types to the last elif in convert_values(). I plan on submitting a patch unless there's a compelling reason not to

Anssi Kääriäinen

unread,
Sep 23, 2011, 3:14:30 AM9/23/11
to django-d...@googlegroups.com, Joshua Russo
My feeling is that there should be better support for custom aggregates. For example array_agg for PostgreSQL would be really useful, but it is impossible to write one currently. I think that the best approach would be that the aggregate does the conversion to the Python world. This would be analogous to models.Field.to_python. This would still not resolve your problem completely, but you could then write class DateMax(aggregates.Max) which would return the value as Date and similarly for the Char case..

That being said, I think you could handle your current situation with .extra(). I usually solve similar situations by doing the join in Python world, which is probably the cleanest solution you can get currently.

It would also be very useful to have .annotate(event=ModelAnnotation(Event, through='fk_field', only=Q(eventType=3))). I had something like that written ages ago, but it was a bit ugly... And it would be cool to have similar FieldAnnotation(). Given some thought and some more work those methods could handle 90%+ of the situations where extra is currently needed. But this is stuff for another post...

 - Anssi
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-developers/-/mGzHtBFTeOEJ.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.

Reply all
Reply to author
Forward
0 new messages