Fixed #7589 -- Added a way for post-table-creation SQL modifications to be done for custom fields (needed by geo-django, but useful in other situations, too).

Patch from Justin Bronn.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8133 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-07-29 05:53:44 +00:00
parent af7773f280
commit 548ac72207
3 changed files with 14 additions and 6 deletions

View File

@ -7,4 +7,4 @@ class Command(AppCommand):
def handle_app(self, app, **options): def handle_app(self, app, **options):
from django.core.management.sql import sql_custom from django.core.management.sql import sql_custom
return u'\n'.join(sql_custom(app)).encode('utf-8') return u'\n'.join(sql_custom(app, self.style)).encode('utf-8')

View File

@ -116,7 +116,7 @@ class Command(NoArgsCommand):
app_name = app.__name__.split('.')[-2] app_name = app.__name__.split('.')[-2]
for model in models.get_models(app): for model in models.get_models(app):
if model in created_models: if model in created_models:
custom_sql = custom_sql_for_model(model) custom_sql = custom_sql_for_model(model, self.style)
if custom_sql: if custom_sql:
if verbosity >= 1: if verbosity >= 1:
print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name) print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name)

View File

@ -219,7 +219,7 @@ def sql_flush(style, only_django=False):
statements = connection.ops.sql_flush(style, tables, sequence_list()) statements = connection.ops.sql_flush(style, tables, sequence_list())
return statements return statements
def sql_custom(app): def sql_custom(app, style):
"Returns a list of the custom table modifying SQL statements for the given app." "Returns a list of the custom table modifying SQL statements for the given app."
from django.db.models import get_models from django.db.models import get_models
output = [] output = []
@ -228,7 +228,7 @@ def sql_custom(app):
app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
for model in app_models: for model in app_models:
output.extend(custom_sql_for_model(model)) output.extend(custom_sql_for_model(model, style))
return output return output
@ -242,7 +242,7 @@ def sql_indexes(app, style):
def sql_all(app, style): def sql_all(app, style):
"Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module." "Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
return sql_create(app, style) + sql_custom(app) + sql_indexes(app, style) return sql_create(app, style) + sql_custom(app, style) + sql_indexes(app, style)
def sql_model_create(model, style, known_models=set()): def sql_model_create(model, style, known_models=set()):
""" """
@ -426,7 +426,7 @@ def many_to_many_sql_for_model(model, style):
return final_output return final_output
def custom_sql_for_model(model): def custom_sql_for_model(model, style):
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
@ -434,6 +434,14 @@ def custom_sql_for_model(model):
app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql')) app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))
output = [] output = []
# Post-creation SQL should come before any initial SQL data is loaded.
# However, this should not be done for fields that are part of a a parent
# model (via model inheritance).
nm = opts.init_name_map()
post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')]
for f in post_sql_fields:
output.extend(f.post_create_sql(style, model._meta.db_table))
# Some backends can't execute more than one SQL statement at a time, # Some backends can't execute more than one SQL statement at a time,
# so split into separate statements. # so split into separate statements.
statements = re.compile(r";[ \t]*$", re.M) statements = re.compile(r";[ \t]*$", re.M)