How to download Django tools from all applications?

I use fixtures in my Django application, but only two applications load their fixtures.

When I manually start loaddata with --verbosity = 2, I see that it only looks in two applications, although I have more with the directory files created inside.

All applications are correctly installed in settings.py.

From the documentation, it seems that Django should look in the fixtures / directory for each installed application.

Any ideas why some apps are being ignored?

+4
source share
4 answers

Source_dates are imported every time you run syncdb. As far as I remember, it also overwrites any changes you made manually.

To load other fixtures, you must use manage.py loaddata fixturename. This works well if you use a common naming scheme in all of your applications. If you do not, you must either provide loaddata with the name of each of them or use find to get a list of fixtures and boot exec data in each of them:

EDIT: (as I add manage.py to / bin in virtualenv, when I install the django package, I use only manage.py, if you don't need it, you will need python manage.py loaddata)

find . -name "*.json" -exec manage.py loaddata {} \; 

I use this in fabfile to automate intermediate installations:

 def load_all_fixtures(): """Loads all the fixtures in every dir""" with cd(env.directory): run(""" source /usr/local/bin/virtualenvwrapper.sh && workon %s && find -L . -name "*.json" -exec manage.py loaddata {} \; """ % env.virtualenv ) 
+5
source

Try to name this way.

 python manage.py loaddata initial_data 

OR programmatically you can call it like

 from django.core.management import call_command call_command('loaddata', 'initial_data', verbosity=3, database='default') 
+3
source

You must put the data in the initial_data file. [json | xml, ...].

I think that by default only those files are downloaded.

AppDir / devices / initial_data.json

+2
source

The problem is that Django only searches for applications in applications that provide a model . Perhaps you have an application that does not have a model, but you still want to download some devices (maybe sample data for another installed application).

In this case, the culprit for this behavior in Django is get_apps() in loaddata.py :

To trick Django into looking at your application folder <app>/fixtures/ , you must add a (empty) models.py file to the application. (Be nice and put a comment in this file so that everything is clear!)

<application> /models.py

 """ No real model, just an empty file to make Django load the fixtures. """ 

Subsequently, running python manage.py loaddata <fixture> will manually find the app application file.

+2
source

Source: https://habr.com/ru/post/1341967/


All Articles