Skip to content

Instantly share code, notes, and snippets.

@pavelpatrin
Last active October 11, 2017 18:52
Show Gist options
  • Save pavelpatrin/0e4c9a4d038fb999f24055374fb0fe1d to your computer and use it in GitHub Desktop.
Save pavelpatrin/0e4c9a4d038fb999f24055374fb0fe1d to your computer and use it in GitHub Desktop.
class RawJoin(Join):
u"""Позволяет внедрить в QuerySet произвольный JOIN.
banners_qs = Banner.objects.extra(where=['my_join_table.status = 123'])
Join('LEFT JOIN (XXX) b1 ON b1.id = abcd ', ()).inject(banners_qs)
"""
def __init__(self, raw_sql, params):
# Используемые опции.
self.raw_sql = raw_sql
self.params = params
# Соответствие интерфейса.
self.table_alias = 'raw_join'
self.table_name = None
self.parent_alias = None
self.join_field = None
self.join_cols = None
self.join_type = None
self.nullable = None
def as_sql(self, compiler, connection):
return self.raw_sql, self.params
def inject(self, queryset):
queryset.query.get_initial_alias()
queryset.query.table_alias(self.table_alias, True)
queryset.query.alias_map[self.table_alias] = self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment