createsuperuser_if_none_exists.py
28 lines
| 991 B
| text/x-python
|
PythonLexer
r366 | from django.core.management.base import BaseCommand | ||
from django.contrib.auth import get_user_model | |||
class Command(BaseCommand): | |||
""" | |||
Create a superuser if none exist | |||
Example: | |||
manage.py createsuperuser_if_none_exists --user=admin123 --password=admin123 --email=admin123@igp.gob.pe | |||
""" | |||
def add_arguments(self, parser): | |||
parser.add_argument("--user", required=True) | |||
parser.add_argument("--password", required=True) | |||
parser.add_argument("--email", default="admin123@igp.gob.pe") | |||
def handle(self, *args, **options): | |||
User = get_user_model() | |||
username = options["user"] | |||
password = options["password"] | |||
email = options["email"] | |||
if User.objects.exists(): | |||
self.stdout.write(f'Local user "{username}" currently exists') | |||
return | |||
User.objects.create_superuser(username=username, password=password, email=email) | |||
self.stdout.write(f'Local user "{username}" was created') |