I am having trouble loading Django fixtures into my MySQL database because of contenttypes conflicts. First I tried dumping the data from only my app like this:
./manage.py dumpdata escola > fixture.json
but I kept getting missing foreign key problems, because my app "escola" uses tables from other applications. I kept adding additional apps until I got to this:
./manage.py dumpdata contenttypes auth escola > fixture.json
Now the problem is the following constraint violation when I try to load the data as a test fixture:
IntegrityError: (1062, "Duplicate entry 'escola-t23aluno' for key 2")
It seems the problem is that Django is trying to dynamically recreate contenttypes with different primary key values that conflict with the primary key values from the fixture. This appears to be the same as bug documented here: http://code.djangoproject.com/ticket/7052
The problem is that the recommended workaround is to dump the contenttypes app which I'm already doing!? What gives? If it makes any difference I do have some custom model permissions as documented here: http://docs.djangoproject.com/en/dev/ref/models/options/#permissions
You must create a directory in your app named fixtures and put your fixtures files there. You can write them in json or xml, one easy way to make them is to create some objects in the admin interface and then run manage.py dumpdata. That would dump the data from the objects you created into fixture files.
A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you've already got some data is to use the manage.py dumpdata command.
dumpdata command It is a django management command, which can be use to backup(export) you model instances or whole database.
manage.py dumpdata --natural
will use a more durable representation of foreign keys. In django they are called "natural keys". For example:
Permission.codename
is used in favour of Permission.id
User.username
is used in favour of User.id
Read more: natural keys section in "serializing django objects"
Some other useful arguments for dumpdata
:
--indent=4
make it human readable.-e sessions
exclude session data-e admin
exclude history of admin actions on admin site-e contenttypes -e auth.Permission
exclude objects which are recreated automatically from schema every time during syncdb
. Only use it together with --natural
or else you might end up with badly aligned id numbers.The answers here all old... As of 2017, the best answer is:
manage.py dumpdata --natural-foreign --natural-primary -e contenttypes -e auth.Permission --indent 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With