Ticket #6940: orm-count-fix.patch

File orm-count-fix.patch, 1.1 KB (added by m.gajda@…, 7 years ago)
  • django/db/models/query.py

     
    231231
    232232        try:
    233233            select, sql, params = counter._get_sql_clause()
     234
     235            formats_in_select = sum( [ 1 if s else -1 for s in ",".join( select ).split( "%" ) ] ) - 1
    234236        except EmptyResultSet:
    235237            return 0
    236238
     
    238240        if self._distinct:
    239241            id_col = "%s.%s" % (connection.ops.quote_name(self.model._meta.db_table),
    240242                    connection.ops.quote_name(self.model._meta.pk.column))
    241             cursor.execute("SELECT COUNT(DISTINCT(%s))" % id_col + sql, params)
     243            cursor.execute("SELECT COUNT(DISTINCT(%s))" % id_col + sql, params[formats_in_select:])
    242244        else:
    243             cursor.execute("SELECT COUNT(*)" + sql, params)
     245            cursor.execute("SELECT COUNT(*)" + sql, params[formats_in_select:])
    244246        count = cursor.fetchone()[0]
    245247
    246248        # Apply any offset and limit constraints manually, since using LIMIT or
Back to Top