File tree 1 file changed +19
-0
lines changed
1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change
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' ))
You can’t perform that action at this time.
0 commit comments