Skip to content

Commit d618ae3

Browse files
committed
Add create_superuser managment script
1 parent 7b88f25 commit d618ae3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/management/create_superuser.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
"""Automatically tries to create a superuser and exits if it already exists."""
3+
import os
4+
from django.core.management.base import BaseCommand
5+
from django.contrib.auth.models import User
6+
7+
class Command(BaseCommand):
8+
help = 'Automatically creates a superuser and exits if it already exists'
9+
10+
def handle(self, *args, **kwargs):
11+
DJANGO_SUPERUSER_USERNAME = os.environ.get('DJANGO_SUPERUSER_USERNAME')
12+
DJANGO_SUPERUSER_EMAIL = os.environ.get('DJANGO_SUPERUSER_EMAIL')
13+
DJANGO_SUPERUSER_PASSWORD = os.environ.get('DJANGO_SUPERUSER_PASSWORD')
14+
15+
if not User.objects.filter(username=DJANGO_SUPERUSER_USERNAME).exists():
16+
User.objects.create_superuser(DJANGO_SUPERUSER_USERNAME, DJANGO_SUPERUSER_EMAIL, DJANGO_SUPERUSER_PASSWORD)
17+
self.stdout.write(self.style.SUCCESS('Superuser created successfully'))
18+
else:
19+
self.stdout.write(self.style.WARNING('Superuser already exists'))

0 commit comments

Comments
 (0)