Created
June 16, 2017 11:31
-
-
Save c00kiemon5ter/7806c1eac8c6a3e82f061ec32a55c702 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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
I have use for this in a project. What is the license of this code? May I simply include it into my repo and apply the projects license (e.g. GPL3)?