[patch] Add a hook for loading SQL files that aren't tied to a particular model
|Reported by:||anonymous||Owned by:||adrian|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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 10 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