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?
very easy, listen on post syncdb signal and read superuser credentials from a configuration file and apply it.
checkout django-bootup
https://github.com/un33k/django-bootup/blob/master/README
This small python script could create a normal user or a superuser
#!/usr/bin/env python
import os
import sys
import argparse
import random
import string
import django
def main(arguments):
parser = argparse.ArgumentParser()
parser.add_argument('--username', dest='username', type=str)
parser.add_argument('--email', dest='email', type=str)
parser.add_argument('--settings', dest='settings', type=str)
parser.add_argument('--project_dir', dest='project_dir', type=str)
parser.add_argument('--password', dest='password', type=str, required=False)
parser.add_argument('--superuser', dest='superuser', action='store_true', required=False)
args = parser.parse_args()
sys.path.append(args.project_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = args.settings
from django.contrib.auth.models import User
django.setup()
username = args.username
email = args.email
password = ''.join(random.sample(string.letters, 20)) if args.password is None else args.password
superuser = args.superuser
try:
user_obj = User.objects.get(username=args.username)
user_obj.set_password(password)
user_obj.save()
except User.DoesNotExist:
if superuser:
User.objects.create_superuser(username, email, password)
else:
User.objects.create_user(username, email, password)
print password
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
--superuser & --password are not mandatory.
If --superuser is not defined, normal user will be created If --password is not defined, a random password will be generated
Ex :
/var/www/vhosts/PROJECT/python27/bin/python /usr/local/sbin/manage_dja_superusertest.py --username USERNAME --email TEST@domain.tld --project_dir /var/www/vhosts/PROJECT/PROJECT/ --settings PROJECT.settings.env
I use './manage.py shell -c':
./manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'adminpass')"
This doesn't uses an extra echo, this has the benefit that you can pass it to a docker container for execution. Without the need to use sh -c "..." which gets you into quote escaping hell.
And remember that first comes username, than the email.
If you have a custom user model you need to import that and not auth.models.User
I would suggest running a Data Migration, so when migrations are applied to the project, a superuser is created as part of the migrations. The username and password can be setup as environment variables. This is also useful when running an app in a container (see this thread as an example)
Your data migration would then look like this:
import os
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('<your_app>', '<previous_migration>'),
] # can also be emtpy if it's your first migration
def generate_superuser(apps, schema_editor):
from django.contrib.auth.models import User
DJANGO_DB_NAME = os.environ.get('DJANGO_DB_NAME', "default")
DJANGO_SU_NAME = os.environ.get('DJANGO_SU_NAME')
DJANGO_SU_EMAIL = os.environ.get('DJANGO_SU_EMAIL')
DJANGO_SU_PASSWORD = os.environ.get('DJANGO_SU_PASSWORD')
superuser = User.objects.create_superuser(
username=DJANGO_SU_NAME,
email=DJANGO_SU_EMAIL,
password=DJANGO_SU_PASSWORD)
superuser.save()
operations = [
migrations.RunPython(generate_superuser),
]
Hope that helps!
EDIT:
Some might raise the question how to set these environment variables and make Django aware of them. There are a lot of ways and it's been answered in other SO posts, but just as a quick pointer, creating a .env
file is a good idea. You could then use the python-dotenv package, but if you have setup a virtual environment with pipenv, it will automatically set the envvars in your .env
file. Likewise, running your app via docker-compose can read in your .env
file.