#2479: [patch] add sum() method to query set ( SELECT SUM( fieldname .... )
-----------------------------------------------+----------------------------
Reporter: Simon Greenhill, [EMAIL PROTECTED] | Owner: adrian
Type: enhancement | Status: new
Priority: normal | Milestone:
Component: Database wrapper | Version: SVN
Severity: minor | Resolution:
Keywords: |
-----------------------------------------------+----------------------------
Comment (by [EMAIL PROTECTED]):
Ok - I'll let you guys give query.py a thorough workover, and have a think
about the best way to implement more of these functions.
Re: filtering - .filter() still works, so you can still get a SELECT SUM(
something ) FROM table WHERE x=a, y=b etc. If you mean something like SUM(
DISTINCT fieldname ), that doesn't work - but that'll only work on MySQL
>5 anyway.
Certainly things like COUNT() are so common that they should have their
own method, but I agree that having one method per database function is
bad DRY. I've quickly had a go at something like this (diff attached)
which adds .aggregate to the methods e.g.
Something.objects.all().aggregate( 'function_name', 'field_name' ). You
could also match the filter syntax, e.g. fieldname_ _function ->
function(fieldname) if preferred.
As for sanity checks - you could definitely check for int/double ( & their
Django Model equivalents ), and raise something like a FieldIsWrongType
exception if it's one of the char/varchar or datetime fields. However, do
you want to stop these being executed on non-numeric fields right across
the board? or is this going to be something to specify like SUM works on
Integer/Float/Decimal/etc Fields, whilst OTHERFUNCTION only works on, say,
IntegerFields.
( Aside - are there any database aggregate functions that are useful on
non-numeric fields? )
re: type casting - point taken! :)
--
Ticket URL: <http://code.djangoproject.com/ticket/2479>
Django <http://code.djangoproject.org/>
The web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" 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-updates
-~----------~----~----~----~------~----~------~--~---