Login

QLeftOuterJoins

Author:
dottedmag
Posted:
June 10, 2007
Language:
Python
Version:
.96
Score:
6 (after 6 ratings)

This hack replaces all INNER JOINs inside to the LEFT OUTER JOINs (see http://code.djangoproject.com/ticket/3592 for explanation).

Use: QLeftOuterJoins(Q(...) | Q(...) & (Q(...) | ....)).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.