add superuser creation command and update settings for environment variables
This commit is contained in:
0
api/management/commands/__init__.py
Normal file
0
api/management/commands/__init__.py
Normal file
18
api/management/commands/createsuperuser_if_not_exists.py
Normal file
18
api/management/commands/createsuperuser_if_not_exists.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth import get_user_model
|
||||
import os
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Create a superuser if it doesn't already exist"
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
User = get_user_model()
|
||||
username = os.getenv("DJANGO_SUPERUSER_USERNAME")
|
||||
email = os.getenv("DJANGO_SUPERUSER_EMAIL")
|
||||
password = os.getenv("DJANGO_SUPERUSER_PASSWORD")
|
||||
|
||||
if not User.objects.filter(username=username).exists():
|
||||
User.objects.create_superuser(username=username, email=email, password=password)
|
||||
self.stdout.write(self.style.SUCCESS(f"Superuser {username} created"))
|
||||
else:
|
||||
self.stdout.write(self.style.WARNING(f"Superuser {username} already exists"))
|
Reference in New Issue
Block a user