How to manage.py loaddata in Django
Asked Answered
B

3

22

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.

Breathing answered 8/2, 2017 at 23:44 Comment(6)
what are the contents in data.json file?Aftereffect
@Aftereffect all the data in the database. It is supposed to work according to the docs: docs.djangoproject.com/en/1.10/ref/django-admin/…Breathing
did you clear your db before running load data?Aftereffect
@Aftereffect yep -> python manage.py flushBreathing
I had the same issue, and running python manage.py flush solved it! So, to summarize, flush the DB and then use the data.json exported via the command from the accepted answer.Inefficiency
related: https://mcmap.net/q/587644/-contenttype-matching-query-does-not-existDermal
R
46

Try it like this:

python manage.py dumpdata --natural-foreign \
   --exclude=auth.permission --exclude=contenttypes \
   --indent=4 > data.json
Reviel answered 10/2, 2017 at 20:29 Comment(3)
This keeps giving me an error "No installed app with label '--exclude'Kathykathye
Ah, fixed it by adding = to --exclude and --indent, so it turns into --exclude=auth.permission --exclude=contenttypes --indent=4Kathykathye
For those who come here from DRF and error of Could not load authtoken.Token(pk=...: also exclude token objects by --exclude=authtoken.tokenKeating
W
2

Watch out for @receiver in your models. They might be your issue, as they were mine.

I just want to point out my case here. I had a receiver in my model. Basically, to create a few instances for extra data to my user, like so.

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(account=instance)
        Preferences.objects.create(account=instance)

Running loaddata breaks into "duplicate key value" because my fixture have these rows and my receiver tries to do it as well. Commenting those lines out, running the command and uncommenting did the trick for me.

Maybe there is some sort of flag for "disable receivers" that I do not know about. This seems like something too expected to not be solved by the Django crew.

Withindoors answered 14/9, 2022 at 15:3 Comment(1)
The docs (docs.djangoproject.com/en/4.1/ref/django-admin/…) explain that loaddata passes a raw kwarg (set to True) to your signals which you can then use to override your signals.Slowworm
R
0

I also got this error with loaddata:

Could not load contenttypes.ContentType(pk=2): duplicate key value violates unique constraint "django_content_type_app_label_model_76bd3d3b_uniq" DETAIL: Key (app_label, model)=(project, address) already exists.

The problem was that the dump did not have all the database migrations that I had in the target system. So make sure you have the migration level that equals to the dump.

Rabin answered 21/9, 2023 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.