Ticket #2479: query_sum.diff

File query_sum.diff, 1017 bytes (added by dev@…, 18 years ago)
  • db/models/query.py

     
    202202            cursor.execute("SELECT COUNT(*)" + sql, params)
    203203        return cursor.fetchone()[0]
    204204
     205    def sum(self, field):
     206        "Performs a SELECT SUM(field) and returns the sum of the field as an integer."
     207        counter = self._clone()
     208        counter._order_by = ()
     209        counter._offset = None
     210        counter._limit = None
     211        counter._select_related = False
     212        select, sql, params = counter._get_sql_clause()
     213        cursor = connection.cursor()
     214        cursor.execute("SELECT SUM(%s)" % field + sql, params)
     215        return cursor.fetchone()[0] # Type cast this to int or just leave as what the db gave us?
     216
    205217    def get(self, *args, **kwargs):
    206218        "Performs the SELECT and returns a single object matching the given keyword arguments."
    207219        clone = self.filter(*args, **kwargs)
Back to Top