Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage.py loaddata in Django

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.

like image 206
Alejandro Veintimilla Avatar asked Feb 08 '17 23:02

Alejandro Veintimilla


People also ask

What does manage py do in Django?

In addition, manage.py is automatically created in each Django project. It does the same thing as django-admin but also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project's settings.py file.

Where is manage py Django?

The django-admin.py script should be on your system path if you installed Django via its setup.py utility. If it's not on your path, you can find it in site-packages/django/bin within your Python installation.

What is manage py Makemigrations?

makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created apps' model which you add in installed apps. It does not execute those commands in your database file.


1 Answers

Try it like this:

python manage.py dumpdata --natural-foreign \
   --exclude=auth.permission --exclude=contenttypes \
   --indent=4 > data.json
like image 58
ugosan Avatar answered Oct 16 '22 02:10

ugosan