Skip to content

Instantly share code, notes, and snippets.

@0xregulus
Created April 10, 2019 16:32
Show Gist options
  • Save 0xregulus/615da82ec824c185cb7cbc38a40a861e to your computer and use it in GitHub Desktop.
Save 0xregulus/615da82ec824c185cb7cbc38a40a861e to your computer and use it in GitHub Desktop.
Left Join Django 1.9
from django.db.models.sql.datastructures import Join
from django.db.models.fields.related import ForeignObject
from django.db.models.options import Options
from myapp.models import Ace
from myapp.models import Subject
jf = ForeignObject(
to=Subject,
on_delete=lambda: x,
from_fields=[None],
to_fields=[None],
rel=None,
related_name=None
)
jf.opts = Options(Ace._meta)
jf.opts.model = Ace
jf.get_joining_columns = lambda: (("subj", "name"),)
j=Join(
Subject._meta.db_table, Ace._meta.db_table,
'T1', "LEFT JOIN", jf, True)
q=Ace.objects.filter(version=296)
q.query.join(j)
print q.query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment