I have a Django application that has a Setting
model. I've manually added the configuration data to the database through the Django Admin UI/Django Console but I want to package up this data and have it automatically created when an individual creates/upgrades an instance of this app. What are some of the best ways to accomplish this?
I have already looked at:
- Django Migrations Topics Documentation which includes a section on Data Migrations
- shows a data migration but it involves data already existing in a database being updated, not new data being added.
- Django Migration Operations Reference
- shows examples using
RunSQL
andRunPython
but both only when adding a minimal amount of data.
- shows examples using
- How to create database migrations
- looks at moving data between apps.
- How to provide initial data for models
- mentions data migrations covered in the previous docs and providing data with fixtures.
Still I haven't found one that seems to line up well with the use case I'm describing.
I also looked at several StackOverflow Q&As on the topic, but all of these are quite old and may not cover any improvements that occurred in the last 8+ years:
- Programmatically using Django's loaddata
- How can I provide the initial data to a Django model?
- Loading initial data with Django 1.7+ and data migrations
There is a decent amount of settings in this app and the above approaches seem pretty laborious as the amount of data increases.
I have exported the data (to JSON) using fixtures (e.g. manage.py dumpdata
) but I don't want folks to have to run fixtures to insert data when installing the app.
It feels like there should be a really simple way to say, "hey, grab this csv, json, yaml, etc. file and populate the models."
Current Thoughts
Barring better recommendations from everyone here my thought is to load the JSON within a data migration and iterate through inserting the data with RunSQL
or RunPython
. Any other suggestions?