Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 548ac722079c1d2c71dac4c5b366425e35d00def 1 parent af7773f
Malcolm Tredinnick malcolmt authored
2  django/core/management/commands/sqlcustom.py
View
@@ -7,4 +7,4 @@ class Command(AppCommand):
def handle_app(self, app, **options):
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')
2  django/core/management/commands/syncdb.py
View
@@ -116,7 +116,7 @@ def handle_noargs(self, **options):
app_name = app.__name__.split('.')[-2]
for model in models.get_models(app):
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 verbosity >= 1:
print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name)
16 django/core/management/sql.py
View
@@ -219,7 +219,7 @@ def sql_flush(style, only_django=False):
statements = connection.ops.sql_flush(style, tables, sequence_list())
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."
from django.db.models import get_models
output = []
@@ -228,7 +228,7 @@ def sql_custom(app):
app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
for model in app_models:
- output.extend(custom_sql_for_model(model))
+ output.extend(custom_sql_for_model(model, style))
return output
@@ -242,7 +242,7 @@ def sql_indexes(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."
- 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()):
"""
@@ -426,7 +426,7 @@ def many_to_many_sql_for_model(model, style):
return final_output
-def custom_sql_for_model(model):
+def custom_sql_for_model(model, style):
from django.db import models
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'))
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,
# so split into separate statements.
statements = re.compile(r";[ \t]*$", re.M)
Please sign in to comment.
Something went wrong with that request. Please try again.