from django.db.models.query import Q, QAnd, QOr class QLeftOuterJoins(Q): "Replaces all INNER JOINs with LEFT OUTER JOINs inside" def __init__(self, q): self.q = q def __and__(self, other): return QAnd(self, other) def __or__(self, other): return QOr(self, other) def get_sql(self, opts): joins, where, params = self.q.get_sql(opts) for join_name, join in joins.iteritems(): joins[join_name] = (join[0], "LEFT OUTER JOIN", join[2]) return joins, where, params