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?
If you reference User directly, your code will not work in projects where the AUTH_USER_MODEL setting has been changed to a different user model. A more generic way to create the user would be:
echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('admin', 'admin@myproject.com', 'password')" | python manage.py shell
ORIGINAL ANSWER
Here there is a simple version of the script to create a superuser:
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'pass')" | python manage.py shell
As of Django 3.0 you can use default createsuperuser --noinput
command and set all required fields (including password) as environment variables DJANGO_SUPERUSER_PASSWORD
, DJANGO_SUPERUSER_USERNAME
, DJANGO_SUPERUSER_EMAIL
for example. --noinput
flag is required.
This comes from the original docs: https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-createsuperuser
and i've just checked - it works. Now you can easily export those environment vars and add createsuperuser
to your scripts and pipelines.
You could write a simple python script to handle the automation of superuser creation. The User
model is just a normal Django model, so you'd follow the normal process of writing a stand-alone Django script. Ex:
import django
django.setup()
from django.contrib.auth.models import User
u = User(username='unique_fellow')
u.set_password('a_very_cryptic_password')
u.is_superuser = True
u.is_staff = True
u.save()
You can also pass createsuperuser
a few options, namely --noinput
and --username
, which would let you automatically create new superusers, but they would not be able to login until you set a password for them.
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.
Current most voted answer:
An improved version would be:
USER="admin"
PASS="super_password"
MAIL="admin@mail.com"
script="
from django.contrib.auth.models import User;
username = '$USER';
password = '$PASS';
email = '$MAIL';
if User.objects.filter(username=username).count()==0:
User.objects.create_superuser(username, email, password);
print('Superuser created.');
else:
print('Superuser creation skipped.');
"
printf "$script" | python manage.py shell
DJANGO_SUPERUSER_USERNAME=testuser \
DJANGO_SUPERUSER_PASSWORD=testpass \
python manage.py createsuperuser --noinput
Documentation for the createuser command