How to manage.py loaddata in Django

Alejandro Veintimilla picture Alejandro Veintimilla · Feb 9, 2017 · Viewed 10.3k times · Source

I've being fighting with this command for several hours now.

If I do

python manage.py dumpdata --natural-foreign --> data.json

when I loaddata I get the error

Could not load contenttypes.ContentType(pk=19): duplicate key value violates unique constraint "django_content_type_app_label_76bd3d3b_uniq" DETAIL: Key (app_label, model)=(misuper, stockitem) already exists.

Then if I do

python manage.py dumpdata --natural-foreign --exclude=contenttypes --> data.json

I get a similar error but with a ̣auth.Permission object:

Could not load auth.Permission(pk=55): duplicate key value violates unique constraint "auth_permission_content_type_id_01ab375a_uniq"

And if I do

python manage.py dumpdata --natural-foreign --exclude=contenttypes --exclude=auth --> data.json

when I loaddata I get

User matching query does not exist

Of course, I'm excluding the auth table.

So ... WTF can I do to load the data? All my tests depend on this.

I believe the docs are insufficient. I'm stuck here, please help.

Answer

ugosan picture ugosan · Feb 10, 2017

Try it like this:

python manage.py dumpdata --natural-foreign \
   --exclude=auth.permission --exclude=contenttypes \
   --indent=4 > data.json