diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-09-11 02:00:27 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-09-11 02:00:27 +0000 |
| commit | e47cc781d844eea72e677d1d212b2dfd1b0c9225 (patch) | |
| tree | a38b67a85c4a516ca729925ef3e720f6b5fd01ac /django/db/models/sql/query.py | |
| parent | 11fd9f2d849c5ab75f0b57cc57ae5035d6e438d8 (diff) | |
A bug from queryset-refactor days: although the Query class has "group_by" and
"having" attributes, only the former was included in the resulting SQL, meaning
subclasses had to completely duplicate Query.as_sql() if they were using any
kind of grouping filtering on the results.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9007 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models/sql/query.py')
| -rw-r--r-- | django/db/models/sql/query.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index ceaf65cd4d..b4525f1075 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -292,6 +292,11 @@ class Query(object): grouping = self.get_grouping() result.append('GROUP BY %s' % ', '.join(grouping)) + if self.having: + having, h_params = self.get_having() + result.append('HAVING %s' % ', '.join(having)) + params.extend(h_params) + if ordering: result.append('ORDER BY %s' % ', '.join(ordering)) @@ -573,6 +578,24 @@ class Query(object): result.append(str(col)) return result + def get_having(self): + """ + Returns a tuple representing the SQL elements in the "having" clause. + By default, the elements of self.having have their as_sql() method + called or are returned unchanged (if they don't have an as_sql() + method). + """ + result = [] + params = [] + for elt in self.having: + if hasattr(elt, 'as_sql'): + sql, params = elt.as_sql() + result.append(sql) + params.extend(params) + else: + result.append(elt) + return result, params + def get_ordering(self): """ Returns list representing the SQL elements in the "order by" clause. |
