Code

Ticket #2922: extra_joins.diff

File extra_joins.diff, 2.5 KB (added by mir@…, 8 years ago)

promised patch

Line 
1--- a/django/db/models/query.py
2+++ b/django/db/models/query.py
3@@ -85,6 +85,7 @@ class QuerySet(object):
4         self._where = []             # List of extra WHERE clauses to use.
5         self._params = []            # List of params to use for extra WHERE clauses.
6         self._tables = []            # List of extra tables to use.
7+        self._joins = []             # List of extra joins to use.
8         self._offset = None          # OFFSET clause.
9         self._limit = None           # LIMIT clause.
10         self._result_cache = None
11@@ -379,7 +380,7 @@ class QuerySet(object):
12         "Returns a new QuerySet instance with '_distinct' modified."
13         return self._clone(_distinct=true_or_false)
14 
15-    def extra(self, select=None, where=None, params=None, tables=None):
16+    def extra(self, select=None, where=None, params=None, tables=None, joins=None):
17         assert self._limit is None and self._offset is None, \
18                 "Cannot change a query once a slice has been taken"
19         clone = self._clone()
20@@ -387,6 +388,7 @@ class QuerySet(object):
21         if where: clone._where.extend(where)
22         if params: clone._params.extend(params)
23         if tables: clone._tables.extend(tables)
24+        if joins: clone._joins.extend(joins)
25         return clone
26 
27     ########################
28@@ -429,6 +431,7 @@ class QuerySet(object):
29         c._where = self._where[:]
30         c._params = self._params[:]
31         c._tables = self._tables[:]
32+        c._joins = self._joins[:]
33         c._offset = self._offset
34         c._limit = self._limit
35         c.__dict__.update(kwargs)
36@@ -448,6 +451,7 @@ class QuerySet(object):
37         if self._where: combined._where.extend(self._where)
38         if self._params: combined._params.extend(self._params)
39         if self._tables: combined._tables.extend(self._tables)
40+        if self._joins: combined._joins.extend(self._joins)
41         # If 'self' is ordered and 'other' isn't, propagate 'self's ordering
42         if (self._order_by is not None and len(self._order_by) > 0) and \
43            (combined._order_by is None or len(combined._order_by) == 0):
44@@ -490,6 +494,8 @@ class QuerySet(object):
45         if joins:
46             sql.append(" ".join(["%s %s AS %s ON %s" % (join_type, table, alias, condition)
47                             for (alias, (table, join_type, condition)) in joins.items()]))
48+        if self._joins:
49+            sql.append(" ".join(self._joins))
50 
51         # Compose the tables clause into SQL.
52         if tables:
53--
541.4.GIT-dirty
55