summaryrefslogtreecommitdiff
path: root/django/db/models/aggregates.py
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2009-01-15 11:06:34 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2009-01-15 11:06:34 +0000
commitcc4e4d9aee0b3ebfb45bee01aec79edc9e144c78 (patch)
tree2cdba846a105d406ecceff2c02e071c50502d487 /django/db/models/aggregates.py
parent50a293a0c31e7325ebd520338f9c8881f951d8a7 (diff)
Fixed #3566 -- Added support for aggregation to the ORM. See the documentation for details on usage.
Many thanks to: * Nicolas Lara, who worked on this feature during the 2008 Google Summer of Code. * Alex Gaynor for his help debugging and fixing a number of issues. * Justin Bronn for his help integrating with contrib.gis. * Karen Tracey for her help with cross-platform testing. * Ian Kelly for his help testing and fixing Oracle support. * Malcolm Tredinnick for his invaluable review notes. git-svn-id: http://code.djangoproject.com/svn/django/trunk@9742 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models/aggregates.py')
-rw-r--r--django/db/models/aggregates.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/django/db/models/aggregates.py b/django/db/models/aggregates.py
new file mode 100644
index 0000000000..1f676253b5
--- /dev/null
+++ b/django/db/models/aggregates.py
@@ -0,0 +1,66 @@
+"""
+Classes to represent the definitions of aggregate functions.
+"""
+
+class Aggregate(object):
+ """
+ Default Aggregate definition.
+ """
+ def __init__(self, lookup, **extra):
+ """Instantiate a new aggregate.
+
+ * lookup is the field on which the aggregate operates.
+ * extra is a dictionary of additional data to provide for the
+ aggregate definition
+
+ Also utilizes the class variables:
+ * name, the identifier for this aggregate function.
+ """
+ self.lookup = lookup
+ self.extra = extra
+
+ def _default_alias(self):
+ return '%s__%s' % (self.lookup, self.name.lower())
+ default_alias = property(_default_alias)
+
+ def add_to_query(self, query, alias, col, source, is_summary):
+ """Add the aggregate to the nominated query.
+
+ This method is used to convert the generic Aggregate definition into a
+ backend-specific definition.
+
+ * query is the backend-specific query instance to which the aggregate
+ is to be added.
+ * col is a column reference describing the subject field
+ of the aggregate. It can be an alias, or a tuple describing
+ a table and column name.
+ * source is the underlying field or aggregate definition for
+ the column reference. If the aggregate is not an ordinal or
+ computed type, this reference is used to determine the coerced
+ output type of the aggregate.
+ * is_summary is a boolean that is set True if the aggregate is a
+ summary value rather than an annotation.
+ """
+ aggregate = getattr(query.aggregates_module, self.name)
+ query.aggregate_select[alias] = aggregate(col, source=source, is_summary=is_summary, **self.extra)
+
+class Avg(Aggregate):
+ name = 'Avg'
+
+class Count(Aggregate):
+ name = 'Count'
+
+class Max(Aggregate):
+ name = 'Max'
+
+class Min(Aggregate):
+ name = 'Min'
+
+class StdDev(Aggregate):
+ name = 'StdDev'
+
+class Sum(Aggregate):
+ name = 'Sum'
+
+class Variance(Aggregate):
+ name = 'Variance'