I'm trying to create a command similar to createsuperuser
which will take two arguments (username and password)
Its working fine in django 1.7 but not in 1.8. (I'm also using python3.4)
this is the code I wrote
myapp/management/commands/createmysuperuser.py
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User
class Command(BaseCommand):
help = 'Create a super user'
def handle(self, *args, **options):
if len(args) != 2:
raise CommandError('need exactly two arguments for username and password')
username, password = args
u, created = User.objects.get_or_create(username=username)
if created:
u.is_superuser = True
u.is_staff = True
u.set_password(password)
u.save()
else:
raise CommandError("user '%s' already exist" % username)
return "Password changed successfully for user '%s'" % u.username
and when I try to run this command
$ python manage.py createmysuperuser myuser mypassword
I get this error
usage: manage.py createmysuperuser [-h] [--version] [-v {0,1,2,3}]
[--settings SETTINGS]
[--pythonpath PYTHONPATH] [--traceback]
[--no-color]
manage.py createmysuperuser: error: unrecognized arguments: myuser mypassword
but when I dont pass any arguments it raises CommandError
which is expected.
CommandError: need exactly two arguments for username and password