[patch] making distinct=True work with get_count
|Reported by:||anonymous||Owned by:||adrian|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Currently get_count() ignores a distinct=True argument. This patch fixes that.
Unfortunately, company firewall issues don't allow me to use svn, so I don't have an svn-diff patch. Sorry about pasting code into the ticket body. For the same reason, I'm using the 0.91 tarball, not the svn version, though I believe the patch is the same for both (I don't think the method in question has changed). In magic-removal, the same idea should work, but it looks like the syntax would be slightly different.
Here's the changed method in django/core/meta/__init__.py (commented out lines are the originals, with the changed version below them):
def function_get_count(opts, **kwargs): kwargs['order_by'] =  kwargs['offset'] = None kwargs['limit'] = None kwargs['select_related'] = False # _, sql, params = function_get_sql_clause(opts, **kwargs) select, sql, params = function_get_sql_clause(opts, **kwargs) cursor = db.db.cursor() # cursor.execute("SELECT COUNT(*)" + sql, params) cursor.execute("SELECT COUNT(" + (kwargs.get('distinct') and "DISTINCT %s" % select or "*") + ")" + sql, params) return cursor.fetchone()