python - Django custom command error: unrecognized arguments -


i'm trying create command similar createsuperuser take 2 arguments (username , password)

its working fine in django 1.7 not in 1.8. (i'm using python3.4)

this code wrote

myapp/management/commands/createmysuperuser.py

from django.core.management.base import basecommand, commanderror django.contrib.auth.models import user   class command(basecommand):     = 'create super user'      def handle(self, *args, **options):          if len(args) != 2:             raise commanderror('need 2 arguments username , 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' exist" % username)          return "password changed user '%s'" % u.username 

and when try run command

$ python manage.py createmysuperuser myuser mypassword

i 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 dont pass arguments raises commanderror expected.

commanderror: need 2 arguments username , password

in django 1.8 should add arguments command:

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

add_argument() method of argparse documented here.

update: default arguments passed in options parameter handle() method should this:

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

and don't need check length of args list - done argparse. recommended method if want use args argument have use "compatibility mode" , name added argument args:

class command(basecommand):      def add_arguments(self, parser):         parser.add_argument('args')      def handle(self, *args, **options):         if len(args) != 2:             ... 

read "changed in django 1.8" side note in first chapter of docs (right after closepoll.py example).

update2: here 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) 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -