Problems with contenttypes when loading a fixture in Django
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 ofPermission.id
User.username
is used in favour ofUser.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 duringsyncdb
. 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
Yes, this is really irritating. For a while I worked around it by doing a "manage.py reset" on the contenttypes app prior to loading the fixture (to get rid of the automatically-generated contenttypes data that differed from the dumped version). That worked, but eventually I got sick of the hassles and abandoned fixtures entirely in favor of straight SQL dumps (of course, then you lose DB portability).
update - the best answer is to use the --natural
flag to dumpdata
, as noted in an answer below. That flag did not exist yet when I wrote this answer.