Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically using Django's loaddata

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?

like image 291
Deniz Dogan Avatar asked May 20 '09 12:05

Deniz Dogan


People also ask

How do I load all fixtures in Django?

By default, Django only loads fixtures into the default database. Use before_scenario to load the fixtures in all of the databases you have configured if your tests rely on the fixtures being loaded in all of them.

What command-line is used for loading data in Django?

Ans: To load data into Django you have to use the command line Django-admin.py loaddata. The command line will searches the data and loads the contents of the named fixtures into the database.

How do I fix Django admin not recognized?

To fix this, first close the terminal window and relaunch it with administrator privileges. Once you launch the elevated terminal window change directory to where you wish to start your Django project. The command should work.


1 Answers

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.

like image 144
Alex Koshelev Avatar answered Oct 11 '22 10:10

Alex Koshelev