23

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

2 Answers 2

42

In django 1.8 you should add arguments to you command:

class Command(BaseCommand):
    ...
    def add_arguments(self, parser):
        parser.add_argument('username')
        parser.add_argument('password')

add_argument() method of argparse is documented here.

UPDATE: By default arguments are passed in the options parameter so the handle() method should look like this:

def handle(self, *args, **options):
    username = options['username']
    password = options['password']
    ...

And you don't need to check the length of the args list - it is already done by argparse. This is the recommended method but if you want to use the args argument then you have to use the "compatibility mode" and name the added argument as args:

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('args')

    def handle(self, *args, **options):
        if len(args) != 2:
            ...

Read the "Changed in Django 1.8" side note in the first chapter of the docs (right after the closepoll.py example).

UPDATE2: Here is the full working example:

from django.core.management.base import BaseCommand    

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('username')
        parser.add_argument('password')

    def handle(self, *args, **options):
        username = options['username']
        password = options['password']
        return u'Username: %s  Password: %s' % (username, password)
Sign up to request clarification or add additional context in comments.

8 Comments

python manage.py createmysuperuser username myuser password mypasswd this command gives this error error: unrecognized arguments: password mypasswd
I've added add_arguments method just as in your answer and this below line to handle method username, password = options['username'], options['password'] am I doing wrong?
See the updated answer. You should call the command like: python manage.py createmysuperuser myuser mypasswd.
I tried your code but still same error see my final code ideone.com/SbtYSB is there any problem with this?
You forgot to add the add_arguments() method from my first snippet.
|
8

For minimal changes just add the method add_arguments() in Command class.

def add_arguments(self, parser):
    parser.add_argument('args', nargs='*')

You can continue to use args as before.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.