Programmatically using Django's loaddata

Deniz Dogan picture Deniz Dogan · May 20, 2009 · Viewed 15.1k times · Source

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?

Answer

Alex Koshelev picture Alex Koshelev · May 20, 2009

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.