mt_
mt_

Reputation: 129

backing up django database with dumpdata

For testing, I would like to be able to save and restore app state. This would seem to be a very common requirement!

I find that I have to do

python manage.py dumpdata --exclude=contenttypes --exclude=auth > sitedata.json

in order for loaddata (after flush) not to complain about uniqueness violations and such.

At present this is just a magic incantation for me that I found in online searches. I don't find the explanations comprehensible.

I would like to know: first, why I have to exclude auth; second, what contenttypes even is, as well as why I have to exclude it. My concern is not that I can't do what I need to do now, but that I don't understand it and wonder if there are other corners of this procedure waiting to bite me.

Thanks for any information or links.

Upvotes: 1

Views: 858

Answers (2)

Alex Vyushkov
Alex Vyushkov

Reputation: 670

I had not so positive experience using dumpdata for database backup, and it wasn't designed for that apparently. I ended up writing my own management command that calls PostgreSQL pg_dump command. I would recommend using your database dump function directly.

Upvotes: 1

Chiefir
Chiefir

Reputation: 2671

You exclude auth - because there might be some sensitive data, like user's email or password - so there is no real need to backup that data for testing purposes - tester can create its own user (more to that - he cant even now what is the password for your backuped user - password is hashed).

"contenttypes" - this is a table, created by included into Django Content Type Framework.

Instances of ContentType represent and store information about the models installed in your project, and new instances of ContentType are automatically created whenever new models are installed.

It is created automatically and stores, so to say "types" for all models, translated into simple numbers. This is for advanced usage. I don't know exactly why it is recommended to omit while making dump data. May be that means that this table is used not in all projects or something else - so there is no real need to backup it, may be to reduce the backup file size. Or may be when tester make migrations on his computer - contenttypes table will already be created, so there is no need to pass that data.

Upvotes: 0

Related Questions