Enable returning a qs (to allow query parameters)
parent
42ceb2cac2
commit
88009b9e99
|
|
@ -2,6 +2,7 @@ import logging
|
||||||
|
|
||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
|
from django.db.models import QuerySet
|
||||||
|
|
||||||
|
|
||||||
class CreateView(migrations.CreateModel):
|
class CreateView(migrations.CreateModel):
|
||||||
|
|
@ -56,14 +57,20 @@ class CreateView(migrations.CreateModel):
|
||||||
schema_editor.execute(sql, None)
|
schema_editor.execute(sql, None)
|
||||||
|
|
||||||
def _create_standard_view(self, model, schema_editor):
|
def _create_standard_view(self, model, schema_editor):
|
||||||
sql_template = 'CREATE VIEW %(table)s AS %(definition)s'
|
sql_template = 'CREATE VIEW %(table)s AS ' % {'table': schema_editor.quote_name(model._meta.db_table)}
|
||||||
qs = str(model.view())
|
params = []
|
||||||
args = {
|
qs = model.view()
|
||||||
'table': schema_editor.quote_name(model._meta.db_table),
|
if isinstance(qs, QuerySet):
|
||||||
'definition': qs,
|
db = qs.db
|
||||||
}
|
compiler = qs.query.get_compiler(using=db)
|
||||||
sql = sql_template % args
|
sql, qs_params = qs.query.as_sql(compiler, schema_editor.connection)
|
||||||
self._create_view_from_raw_sql(sql, schema_editor)
|
sql_template += sql
|
||||||
|
params += qs_params
|
||||||
|
elif isinstance(qs, str):
|
||||||
|
sql_template += qs
|
||||||
|
else:
|
||||||
|
raise Exception('view() should return a string or a QuerySet')
|
||||||
|
self._create_view_from_raw_sql(sql_template, schema_editor, params=params)
|
||||||
|
|
||||||
def _create_view_from_raw_sql(self, sql, schema_editor):
|
def _create_view_from_raw_sql(self, sql, schema_editor, params=None):
|
||||||
schema_editor.execute(sql, None)
|
schema_editor.execute(sql, params)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue