Programmatically using Django's loaddata

DjangoLoaddata

Django Problem Overview


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?

Django Solutions


Solution 1 - Django

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeniz DoganView Question on Stackoverflow
Solution 1 - DjangoAlex KoshelevView Answer on Stackoverflow