modern-django-starter/apps/accounts/management/commands/create_superuser.py
Django Template 4bbc56a08f Initial commit: Django boilerplate with authentication, Docker, and Dokploy support
 Features:
- Complete Django project with authentication
- Email/password and social login (Google, Facebook)
- Role-based access control (admin, staff, user)
- Docker and Docker Compose setup
- Production-ready configuration
- Static file handling with WhiteNoise
- PostgreSQL database integration
- Comprehensive documentation
- GitHub CI/CD workflows
- Dokploy deployment configuration

🚀 Ready for deployment on Dokploy, Heroku, Railway, and other platforms
2025-09-11 09:36:55 +05:30

43 lines
1.6 KiB
Python

from django.core.management.base import BaseCommand
from django.contrib.auth.models import Group
from apps.accounts.models import User
from decouple import config
class Command(BaseCommand):
help = 'Create a superuser and assign admin role'
def add_arguments(self, parser):
parser.add_argument('--email', type=str, help='Superuser email')
parser.add_argument('--password', type=str, help='Superuser password')
parser.add_argument('--first_name', type=str, help='First name', default='Admin')
parser.add_argument('--last_name', type=str, help='Last name', default='User')
def handle(self, *args, **options):
email = options.get('email') or config('SUPERUSER_EMAIL', default='admin@example.com')
password = options.get('password') or config('SUPERUSER_PASSWORD', default='admin123')
first_name = options.get('first_name', 'Admin')
last_name = options.get('last_name', 'User')
if User.objects.filter(email=email).exists():
self.stdout.write(
self.style.WARNING(f'User with email {email} already exists')
)
return
user = User.objects.create_superuser(
email=email,
username=email,
password=password,
first_name=first_name,
last_name=last_name
)
admin_group, created = Group.objects.get_or_create(name='admin')
user.groups.add(admin_group)
self.stdout.write(
self.style.SUCCESS(
f'Successfully created superuser: {email} with admin role'
)
)