I'd like to call the equivalent of manage.py loaddata
from a Django view. I'd like to be able to specify where to load the data from and which application to load it into.
Any ideas?
I'd like to call the equivalent of manage.py loaddata
from a Django view. I'd like to be able to specify where to load the data from and which application to load it into.
Any ideas?
Each django-admin.py
(manage.py
) command, as seen in the documentation, you can call from your code with:
from django.core.management import call_command
call_command('loaddata', 'myapp')
Where first param is the command name, all other position params are the same as command line position params and all keyword params are options.
loaddata
command directly in a data migration, because it internally loads the most up-to-date model definitions and uses them to deserialize historical data in a fixture. That's incorrect behavior. Solution here: https://mcmap.net/q/204333/-loading-initial-data-with-django-1-7-and-data-migrations –
Intermix app_label
keyword argument to specify where to load the fixture, e.g. call_command('loaddata', 'initial_data.json', app_label='myapp')
. Otherwise, it will load fixtures with the same name from all installed apps. –
Intermix © 2022 - 2024 — McMap. All rights reserved.