I want to auto run manage.py createsuperuser
on django
but it seams that there is no way of setting a default password.
How can I get this?
I was searching for an answer to this myself. I decided to create a Django command which extends the base createsuperuser
command (GitHub):
from django.contrib.auth.management.commands import createsuperuser
from django.core.management import CommandError
class Command(createsuperuser.Command):
help = 'Crate a superuser, and allow password to be provided'
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):
password = options.get('password')
username = options.get('username')
database = options.get('database')
if password and not username:
raise CommandError("--username is required if specifying --password")
super(Command, self).handle(*args, **options)
if password:
user = self.UserModel._default_manager.db_manager(database).get(username=username)
user.set_password(password)
user.save()
Example use:
./manage.py createsuperuser2 --username test1 --password 123321 --noinput --email 'blank@email.com'
This has the advantage of still supporting the default command use, while also allowing non-interactive use for specifying a password.