Ticket #2922: extra_joins.diff

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

promised patch

  • django/db/models/query.py

    a b class QuerySet(object): 
    8585        self._where = []             # List of extra WHERE clauses to use.
    8686        self._params = []            # List of params to use for extra WHERE clauses.
    8787        self._tables = []            # List of extra tables to use.
     88        self._joins = []             # List of extra joins to use.
    8889        self._offset = None          # OFFSET clause.
    8990        self._limit = None           # LIMIT clause.
    9091        self._result_cache = None
    class QuerySet(object): 
    379380        "Returns a new QuerySet instance with '_distinct' modified."
    380381        return self._clone(_distinct=true_or_false)
    381382
    382     def extra(self, select=None, where=None, params=None, tables=None):
     383    def extra(self, select=None, where=None, params=None, tables=None, joins=None):
    383384        assert self._limit is None and self._offset is None, \
    384385                "Cannot change a query once a slice has been taken"
    385386        clone = self._clone()
    class QuerySet(object): 
    387388        if where: clone._where.extend(where)
    388389        if params: clone._params.extend(params)
    389390        if tables: clone._tables.extend(tables)
     391        if joins: clone._joins.extend(joins)
    390392        return clone
    391393
    392394    ########################
    class QuerySet(object): 
    429431        c._where = self._where[:]
    430432        c._params = self._params[:]
    431433        c._tables = self._tables[:]
     434        c._joins = self._joins[:]
    432435        c._offset = self._offset
    433436        c._limit = self._limit
    434437        c.__dict__.update(kwargs)
    class QuerySet(object): 
    448451        if self._where: combined._where.extend(self._where)
    449452        if self._params: combined._params.extend(self._params)
    450453        if self._tables: combined._tables.extend(self._tables)
     454        if self._joins: combined._joins.extend(self._joins)
    451455        # If 'self' is ordered and 'other' isn't, propagate 'self's ordering
    452456        if (self._order_by is not None and len(self._order_by) > 0) and \
    453457           (combined._order_by is None or len(combined._order_by) == 0):
    class QuerySet(object): 
    490494        if joins:
    491495            sql.append(" ".join(["%s %s AS %s ON %s" % (join_type, table, alias, condition)
    492496                            for (alias, (table, join_type, condition)) in joins.items()]))
     497        if self._joins:
     498            sql.append(" ".join(self._joins))
    493499
    494500        # Compose the tables clause into SQL.
    495501        if tables:
Back to Top