Skip to content

Instantly share code, notes, and snippets.

@d-kahara
Forked from c00kiemon5ter/create-superuser.py
Created April 22, 2020 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-kahara/640295ee163222cdc0e1c0a8611f30ae to your computer and use it in GitHub Desktop.
Save d-kahara/640295ee163222cdc0e1c0a8611f30ae to your computer and use it in GitHub Desktop.
Extend Django's management createsuperuser command to allow non-interactive creation of a superuser with a password.
"""
Extend createsuperuser command to allow non-interactive creation of a
superuser with a password.
Instructions:
mkdir -p path-to-your-app/management/commands/
touch path-to-your-app/management/__init__.py
touch path-to-your-app/management/commands/__init__.py
and place this file under path-to-your-app/management/commands/
Example usage:
manage.py create-superuser \
--username foo \
--password foo \
--email foo@foo.foo
"""
from django.contrib.auth.management.commands import createsuperuser
from django.core.management import CommandError
class Command(createsuperuser.Command):
help = 'Create a superuser with a password non-interactively'
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--password', dest='password', default=None,
help='Specifies the password for the superuser.',
)
def handle(self, *args, **options):
options.setdefault('interactive', False)
database = options.get('database')
password = options.get('password')
username = options.get('username')
email = options.get('email')
if not password or not username or not email:
raise CommandError(
"--email --username and --password are required options")
user_data = {
'username': username,
'password': password,
'email': email,
}
self.UserModel._default_manager.db_manager(
database).create_superuser(**user_data)
if options.get('verbosity', 0) >= 1:
self.stdout.write("Superuser created successfully.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment