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) |