Add users to a Django application in batch.

Reading time ~1 minute

As a Django administrator, I often have to create user accounts for a demo or training session. Below is a small script that can be run from the Django shell to create users in batch.

import sys
from django.contrib.auth import authenticate
from django.contrib.auth import get_user_model

User = get_user_model()

#  Update the users in this list.
#  Each tuple represents the username, password, and email of a user.
users = [
    ('user_1', 'phgzHpXcnJ', ''),
    ('user_2', 'ktMmqKcpJw', ''),

for username, password, email in users:
        print 'Creating user {0}.'.format(username)
        user = User.objects.create_user(username=username, email=email)

        assert authenticate(username=username, password=password)
        print 'User {0} successfully created.'.format(username)

        print 'There was a problem creating the user: {0}.  Error: {1}.' \
            .format(username, sys.exc_info()[1])

Using pg_dump instead of Django fixtures to load initial data.

How to use pg_dump instead of fixtures to bootstrap large data sets in a Django application. Continue reading