parent
02c9a9dec0
commit
85fd2537ef
|
|
@ -0,0 +1,40 @@
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
class CreateView(migrations.CreateModel):
|
||||||
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
|
model = to_state.apps.get_model(app_label, self.name)
|
||||||
|
|
||||||
|
if not self.allow_migrate_model(schema_editor.connection.alias, model):
|
||||||
|
raise
|
||||||
|
models = __import__(app_label).models
|
||||||
|
model = getattr(models, self.name)
|
||||||
|
|
||||||
|
sql = 'DROP VIEW IF EXISTS %(table)s;'
|
||||||
|
|
||||||
|
args = {
|
||||||
|
'table' : schema_editor.quote_name(model._meta.db_table),
|
||||||
|
}
|
||||||
|
|
||||||
|
sql = sql % args
|
||||||
|
|
||||||
|
schema_editor.execute(sql, None)
|
||||||
|
|
||||||
|
sql = 'CREATE VIEW %(table)s AS %(definition)s'
|
||||||
|
|
||||||
|
if hasattr(model, 'view'):
|
||||||
|
qs = str(model.view())
|
||||||
|
else:
|
||||||
|
raise Exception('Your view needs to define either view or ' +
|
||||||
|
'get_view_str')
|
||||||
|
|
||||||
|
args['definition'] = qs
|
||||||
|
|
||||||
|
sql = sql % args
|
||||||
|
|
||||||
|
schema_editor.execute(sql, None)
|
||||||
|
|
||||||
|
def database_backwards(self, app_label, schema_editor, from_state, to):
|
||||||
|
model = from_state.apps.get_model(app_label, self.name)
|
||||||
|
sql = 'DROP VIEW IF EXISTS %s' % \
|
||||||
|
schema_editor.quote_name(model._meta.db_table)
|
||||||
|
schema_editor.execute(sql, None)
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
class ViewManager(models.Manager):
|
||||||
|
def bulk_create(self, *args, **kwargs):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def create(self, *args, **kwargs):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def get_or_create(self, *args, **kwargs):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def update(self, *args, **kwargs):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
class View(models.Model):
|
||||||
|
objects = ViewManager()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
raise NotImplementedError
|
||||||
Loading…
Reference in New Issue