With the code that shtuff.it specified, work successfully and create the user but I cannot login to the created user, so I need to modify the code to create the superuser.
P.S: I have changed the order of parameter, like I have put the email before the password and it work for me.
Here is the complete code that we can use to create the django superuser from CLI:
[appname]/managment/commands/add_user.py:
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from optparse import make_option
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--user',
action='store',
default=None,
help='Username for new user'),
make_option('--password',
action='store',
default=None,
help='User Password'),
make_option('--email',
action='store',
default=None,
help='User Email Address'),
)
def handle(self, *args, **kwargs):
user = User.objects.create_superuser(
username = kwargs.get('user'),
email = kwargs.get('email'),
password = kwargs.get('password').strip(),
)
user.save( )
Then use the following command to add the user from CLI:
python manage.py add_user --user=USERNAME --password=PASSWORD --email=mail@test.com