Automatical superuser creation with Django

Published 4th September 2008

I delete and sync my database fairly often during development with Django because the "syncdb" command will not alter the table in the database after, for example, adding a new field to the corresponding model.

The problem I have with this is typing in the same data for a superuser over and over again. It's a very repetitive task, so I was grateful when I heard this tip from my co-worker Sebastian today.

Superuser from fixture

We're going to automatically load the superuser from a fixture. To do this, dump the data of the auth module into a fixture called "initial_data.json":

./manage.py dumpdata --indent=2 auth > initial_data.json

You'll see that along the superuser that you've already created during the usual "syncdb" execution, a few other credentials got dumped. Since we only need the data for the superuser, delete the irrelevant stuff. The file should look like this:

[
  {
    "pk": 1, 
    "model": "auth.user", 
    "fields": {
      "username": "arthur", 
      "first_name": "", 
      "last_name": "", 
      "is_active": true, 
      "is_superuser": true, 
      "is_staff": true, 
      "last_login": "2008-09-04 14:25:29", 
      "groups": [], 
      "user_permissions": [], 
      "password": "sha1$fooobar123", 
      "email": "arthur@arthurkoziel.com", 
      "date_joined": "2008-09-04 14:25:29"
    }
  }
]

The fixture called "initial_data.json" will automatically get loaded by Django every time you execute the "syncdb" command.

Delete your database and try to run the "syncdb" command with the "--noinput" option passed (it will prevent the script to go into interactive mode):

./manage.py syncdb --noinput

There shouldn't be a prompt for a superuser and you should see a message at the end of the output indicating that your fixture was loaded.

Admin login

Not having to create a superuser is great, but if you're working a lot with Django's contrib.admin application, you'll need to log-in again every time you sync the database and load the user fixture. Another repetitive task that can be eliminated:

After logging in into the admin backend, dump the data of the "session" table into stdout:

./manage.py dumpdata --indent=2 sessions

Copy the dictionary containing the session for your superuser and append it to the list in "initial_data.json" like this:

[
  {
    "pk": 1, 
    "model": "auth.user", 
    "fields": {
      "username": "arthur", 
      "first_name": "", 
      "last_name": "", 
      "is_active": true, 
      "is_superuser": true, 
      "is_staff": true, 
      "last_login": "2008-09-04 14:25:29", 
      "groups": [], 
      "user_permissions": [], 
      "password": "sha1$foobarbarfoo", 
      "email": "arthur@arthurkoziel.com", 
      "date_joined": "2008-09-04 14:25:29"
    }
  },
  {
    "pk": "9aadfe1de61b0937fasd684221f03", 
    "model": "sessions.session", 
    "fields": {
      "expire_date": "2008-10-20 14:34:59", 
      "session_data": "foobar123"
    }
  }
]

You might want to increase the "expire_date" a little bit, so that your session won't expire.

Now every time you delete and sync your database (remember to pass "--noinput"), Django will automatically load the superuser and it's associated session from the fixture. You won't have to manually type in the data for the user and log-in into the backend everytime anymore.