Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored July 29, 2008
2  django/core/management/commands/sqlcustom.py
@@ -7,4 +7,4 @@ class Command(AppCommand):
7 7
 
8 8
     def handle_app(self, app, **options):
9 9
         from django.core.management.sql import sql_custom
10  
-        return u'\n'.join(sql_custom(app)).encode('utf-8')
  10
+        return u'\n'.join(sql_custom(app, self.style)).encode('utf-8')
2  django/core/management/commands/syncdb.py
@@ -116,7 +116,7 @@ def handle_noargs(self, **options):
116 116
             app_name = app.__name__.split('.')[-2]
117 117
             for model in models.get_models(app):
118 118
                 if model in created_models:
119  
-                    custom_sql = custom_sql_for_model(model)
  119
+                    custom_sql = custom_sql_for_model(model, self.style)
120 120
                     if custom_sql:
121 121
                         if verbosity >= 1:
122 122
                             print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name)
16  django/core/management/sql.py
@@ -219,7 +219,7 @@ def sql_flush(style, only_django=False):
219 219
     statements = connection.ops.sql_flush(style, tables, sequence_list())
220 220
     return statements
221 221
 
222  
-def sql_custom(app):
  222
+def sql_custom(app, style):
223 223
     "Returns a list of the custom table modifying SQL statements for the given app."
224 224
     from django.db.models import get_models
225 225
     output = []
@@ -228,7 +228,7 @@ def sql_custom(app):
228 228
     app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
229 229
 
230 230
     for model in app_models:
231  
-        output.extend(custom_sql_for_model(model))
  231
+        output.extend(custom_sql_for_model(model, style))
232 232
 
233 233
     return output
234 234
 
@@ -242,7 +242,7 @@ def sql_indexes(app, style):
242 242
 
243 243
 def sql_all(app, style):
244 244
     "Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
245  
-    return sql_create(app, style) + sql_custom(app) + sql_indexes(app, style)
  245
+    return sql_create(app, style) + sql_custom(app, style) + sql_indexes(app, style)
246 246
 
247 247
 def sql_model_create(model, style, known_models=set()):
248 248
     """
@@ -426,7 +426,7 @@ def many_to_many_sql_for_model(model, style):
426 426
 
427 427
     return final_output
428 428
 
429  
-def custom_sql_for_model(model):
  429
+def custom_sql_for_model(model, style):
430 430
     from django.db import models
431 431
     from django.conf import settings
432 432
 
@@ -434,6 +434,14 @@ def custom_sql_for_model(model):
434 434
     app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))
435 435
     output = []
436 436
 
  437
+    # Post-creation SQL should come before any initial SQL data is loaded.
  438
+    # However, this should not be done for fields that are part of a a parent
  439
+    # model (via model inheritance).
  440
+    nm = opts.init_name_map()
  441
+    post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')]
  442
+    for f in post_sql_fields:
  443
+        output.extend(f.post_create_sql(style, model._meta.db_table))
  444
+
437 445
     # Some backends can't execute more than one SQL statement at a time,
438 446
     # so split into separate statements.
439 447
     statements = re.compile(r";[ \t]*$", re.M)

0 notes on commit 548ac72

Please sign in to comment.
Something went wrong with that request. Please try again.