This python script shows how to add a super user and display all super users in your Django 1.6 installation from the command line. I wrote it to support automated configuration of my django installations.
The script file must reside in the settings.py directory but that can be changed by changing the ‘settings’ argument at line 9.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Usage: # $ # set your virtual environment if necessary # $ cd <django-project>/<project> # $ python make-user bigbob secret true import os import sys # The new way to setup your environment. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') from django.contrib.auth.models import User # No fancy error checking. assert len(sys.argv) == 4 username = sys.argv[1] password = sys.argv[2] superuser = sys.argv[3] # true or false # Create the user, make them a superuser if the 3rd # argument is true. user = User(username='%s' % (username)) user.set_password('%s' % (password)) if superuser.lower() in ['true', 't']: user.is_superuser = True user.is_staff = True user.save() # List the superusers. from django.db.models import Q superusers = [str(user) for user in User.objects.filter(Q(is_superuser=True)).distinct()] print('%d superusers: %s' % (len(superusers), ', '.join(superusers))) |
Note that there are many other user fields (like email) that it does not set.
I hope that you find it helpful.