Create Initial data via database API instead of SQL
|Reported by:||anonymous||Owned by:||adrian|
|Component:||Core (Management commands)||Version:|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The ability to insert initial data via SQL is documented at http://www.djangoproject.com/documentation/model_api/#providing-initial-sql-data. However, Django already provides the OR mapper and database API, and it would be nice to create initial data using those tools. The initial SQL data documentation discusses putting ".sql" files in <appname>/sql/<modelname>.sql. I would like to add the ability to execute special files in a similar location (i.e., <appname>/SOMEPLACE/<modelname>.py, where SOMEPLACE is "data" or "load" or something like that), so that database API calls like the following could be used (given the appropriate import statements for each):
# Mymodel2 has an "other" field, which is a Foreign Key field (to Mymodel)
I think the syntax looks a little wordy (maybe an opportunity for a convenience method on the Model class?), but it allows my to create objects, including relationships among the model classes.
In this scenario, there would need to be a way to ensure the order of execution, or we would need to be able to put creation statements for multiple models in a single script file.