4

I am using the method described in this question to load intial users for my django project.

This saves user permissions, where one permission record might look like this:

{
"pk": 56, 
"model": "auth.permission", 
"fields": {
  "codename": "change_somemodel", 
  "name": "Can change some model", 
  "content_type": 19
}

And a user record:

{
"pk": 2, 
"model": "auth.user", 
"fields": {
  "username": "some_user", 
  "first_name": "", 
  "last_name": "", 
  "is_active": true, 
  "is_superuser": false, 
  "is_staff": true, 
  "last_login": "2011-09-20 06:36:54", 
  "groups": [], 
  "user_permissions": [
    10, 
    11, 
    19, 
    20, 
    21, 
    1, 
    2, 
    56,
    ...,
  ], 
  "password": "sha1$e4f29$fcec7f8bb930d98abdaaa3c0020220f413c4c1f5", 
  "email": "", 
  "date_joined": "2011-03-15 06:01:41"
}

Is there any potential for the content type foreign key to change on a future installation? How about if models or apps added? For example, let's say I add a model to my core app, then I have some reusable apps which are listed after that in settings.py, will those have a different content_type_id on a new installation? Can I include the content_type table in my initial data, or is that likely to cause other issues?

If this isn't a reliable method to load multiple initial users into the database, what are the alternatives?

Community
  • 1
  • 1
AgDude
  • 1,167
  • 1
  • 10
  • 27

2 Answers2

7

In practice, it may be that you also have to exclude contenttypes to make this work. Additionally, if you have any user or group data, you'll need to dump those as well. See this thread for more info on excluding contenttypes.

For my app, I had to use the following command for loaddata or test to work with the fixtures generated by manage.py dumpdata. Additionally, you should probably use --indent N option to make sure that you get a human readable file (where N is the number of spaces for indentation)

./manage.py dumpdata -n --indent 4 -e contenttypes appName auth.Group auth.User > initial_data.json
Community
  • 1
  • 1
crlane
  • 521
  • 5
  • 16
7

Check Natural Keys . Use -n w/ dumpdata, like ./manage.py dumpdata -n auth.User

cgl
  • 1,106
  • 1
  • 13
  • 27
okm
  • 23,575
  • 5
  • 83
  • 90
  • Thanks for the pointer. I was not aware of natural keys. In this case however, it doesn't seem to work. I used dumpdata to get only data from the 'auth' app, and I get this error on syncdb: IntegrityError: columns content_type_id, codename are not unique. – AgDude Mar 30 '12 at 18:38
  • I solved this issue. Thanks okm. I didn't follow your instructions close enough the first time, and was trying to load permissions as well as users from a fixture. – AgDude Mar 31 '12 at 13:47