Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#1813 closed defect (wontfix)

[patch] Add a hook for loading SQL files that aren't tied to a particular model

Reported by: anonymous Owned by: adrian
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

hi.
I have a application which needs to insert data into tables of other applications.
In my case it is to store defaults into preferences.

The app doesn't have any models (see #1812 as well), so I can't just add it onto a different model.

the following patch allows django to look for a unique SQL file for these cases.

Index: management.py
===================================================================
--- management.py       (revision 2864)
+++ management.py       (working copy)
@@ -342,11 +342,21 @@
 def get_sql_initial_data(app):
     "Returns a list of the initial INSERT SQL statements for the given app."
     from django.db.models import get_models
+    from django.conf import settings
     output = []
 
     app_models = get_models(app)
     app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
 
+    app_sql_files = [ os.path.join(app_dir,"%s.sql" % (app.__name__)),
+                      os.path.join(app_dir,"%s.%s.sql" % (app.__name__, settings.DATABASE_ENGINE)) ]
+    for sql_file in app_sql_files:
+        if os.path.exists(sql_file):
+            fp = open(sql_file)
+            output.append(fp.read())
+            fp.close()
+
+
     for klass in app_models:
         output.extend(get_sql_initial_data_for_model(klass))

Change History (3)

comment:1 Changed 9 years ago by adrian

  • Summary changed from [patch] sqlinitialdata should be able to load non-model SQL to [patch] Add a hook for loading SQL files that aren't tied to a particular model

comment:2 Changed 9 years ago by adrian

  • Status changed from new to assigned

comment:3 Changed 9 years ago by adrian

  • Resolution set to wontfix
  • Status changed from assigned to closed

This is a bit too much of a special case.

Note: See TracTickets for help on using tickets.
Back to Top