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
On 09/23/2011 03:16 AM, Joshua Russo wrote:
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
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django
developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en.