Create a custom command:
yourapp/management/commands/createadmin.py
Python:
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
class Command(BaseCommand):
def handle(self, *args, **kwargs):
User = get_user_model()
if not User.objects.filter(username='admin').exists():
User.objects.create_superuser(
'admin', 'admin@example.com', 'StrongPassword123!'
)
self.stdout.write("Superuser created")
else:
self.stdout.write("Superuser already exists")
Run:
python manage.py createadmin
VIDEO: