web/lib/django/db/models/aggregates.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 """
       
     2 Classes to represent the definitions of aggregate functions.
       
     3 """
       
     4 
       
     5 class Aggregate(object):
       
     6     """
       
     7     Default Aggregate definition.
       
     8     """
       
     9     def __init__(self, lookup, **extra):
       
    10         """Instantiate a new aggregate.
       
    11 
       
    12          * lookup is the field on which the aggregate operates.
       
    13          * extra is a dictionary of additional data to provide for the
       
    14            aggregate definition
       
    15 
       
    16         Also utilizes the class variables:
       
    17          * name, the identifier for this aggregate function.
       
    18         """
       
    19         self.lookup = lookup
       
    20         self.extra = extra
       
    21 
       
    22     def _default_alias(self):
       
    23         return '%s__%s' % (self.lookup, self.name.lower())
       
    24     default_alias = property(_default_alias)
       
    25 
       
    26     def add_to_query(self, query, alias, col, source, is_summary):
       
    27         """Add the aggregate to the nominated query.
       
    28 
       
    29         This method is used to convert the generic Aggregate definition into a
       
    30         backend-specific definition.
       
    31 
       
    32          * query is the backend-specific query instance to which the aggregate
       
    33            is to be added.
       
    34          * col is a column reference describing the subject field
       
    35            of the aggregate. It can be an alias, or a tuple describing
       
    36            a table and column name.
       
    37          * source is the underlying field or aggregate definition for
       
    38            the column reference. If the aggregate is not an ordinal or
       
    39            computed type, this reference is used to determine the coerced
       
    40            output type of the aggregate.
       
    41          * is_summary is a boolean that is set True if the aggregate is a
       
    42            summary value rather than an annotation.
       
    43         """
       
    44         klass = getattr(query.aggregates_module, self.name)
       
    45         aggregate = klass(col, source=source, is_summary=is_summary, **self.extra)
       
    46         query.aggregates[alias] = aggregate
       
    47 
       
    48 class Avg(Aggregate):
       
    49     name = 'Avg'
       
    50 
       
    51 class Count(Aggregate):
       
    52     name = 'Count'
       
    53 
       
    54 class Max(Aggregate):
       
    55     name = 'Max'
       
    56 
       
    57 class Min(Aggregate):
       
    58     name = 'Min'
       
    59 
       
    60 class StdDev(Aggregate):
       
    61     name = 'StdDev'
       
    62 
       
    63 class Sum(Aggregate):
       
    64     name = 'Sum'
       
    65 
       
    66 class Variance(Aggregate):
       
    67     name = 'Variance'