Ticket #3732: count_distinct.patch

File count_distinct.patch, 1.2 KB (added by David S. <davidschein@…>, 18 years ago)

patch

  • query.py

     
    223223
    224224        cursor = connection.cursor()
    225225        if self._distinct:
    226             id_col = "%s.%s" % (backend.quote_name(self.model._meta.db_table),
    227                     backend.quote_name(self.model._meta.pk.column))
    228             cursor.execute("SELECT COUNT(DISTINCT(%s))" % id_col + sql, params)
     226            if self._fields:
     227                columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields]
     228                select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns]
     229                distinct_expr = "||".join(select)
     230                cursor.execute("SELECT COUNT(DISTINCT(%s))" % distinct_expr + sql, params)
     231            else:
     232                id_col = "%s.%s" % (backend.quote_name(self.model._meta.db_table),
     233                        backend.quote_name(self.model._meta.pk.column))
     234                cursor.execute("SELECT COUNT(DISTINCT(%s))" % id_col + sql, params)
    229235        else:
    230236            cursor.execute("SELECT COUNT(*)" + sql, params)
    231237        count = cursor.fetchone()[0]
Back to Top