5

I have the following user model,

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(unique=True, max_length=255)
    mobile = PhoneNumberField(null=True)
    username = models.CharField(null=False, unique=True, max_length=255)
    full_name = models.CharField(max_length=255, blank=True, null=True)
    is_bot = models.BooleanField(default=False)

I want to create a custom command which could work like createsuperuser and creates a bot.

I have created a management package in the relevant app and added a command package inside that and a file createbot.py inside of that.

This is my code inside createbot.py

class Command(BaseCommand):
    def handle(self, email, username=None, password=None):
        user = User.objects.create(email,
                                   username=username,

                                   password=password,
                                   is_staff=True,
                                   is_superuser=True,
                                   is_active=True,
                                   is_bot=True
                                   )
        self.stdout.write(self.style.SUCCESS('Successfully create user bot with id: {}, email: {}'.format(user.id, user.email)))

I want this to work exactly like createsuper user giving me prompts to enter email, name and the works. But when I run it, I get the following,

TypeError: handle() got an unexpected keyword argument 'verbosity'

How can I get this to work?

2
  • How did you invoke the command? What did you write on the command line? Commented May 25, 2018 at 21:22
  • Note, you need to be using create_user not just create, otherwise the password won't be hashed. Commented May 25, 2018 at 21:35

1 Answer 1

4

Like is specified in the documentation on creating custom commands:

In addition to being able to add custom command line options, all management commands can accept some default options such as --verbosity and --traceback.

So that means the handle(..) function is invoked with those parameters, even if you are not interested in these.

You can however easily catch those and ignore them, by making using of keyword arguments:

class Command(BaseCommand):

    def handle(self, email, username=None, password=None, **other):
        # ...
        # perform actions
        pass

Here other is a dictionary that maps strings to values: the parameters with which the function is called, but that are not explicitly mentioned in the signature of the function.

The documentation also mentions how to specify the parameters you want to use in the handle, such that helptext can be generated when the user requests how to use the custom command. You can for example write:

class Command(BaseCommand):

    def add_arguments(self, parser):
        # Positional arguments
        parser.add_argument('email', required=True)

        # Named (optional) arguments
        parser.add_argument(
            '--username',
            help='The username for the user',
        )
        parser.add_argument(
            '--password',
            help='The password for the user',
        )

    def handle(self, email, username=None, password=None, **other):
        # ...
        # perform actions
        pass

Note that passwords are hashed in Django, hence you should use create_user(..).

Sign up to request clarification or add additional context in comments.

5 Comments

How do I invoke this on the command line. I can't get it to work.
@MelissaStewart: manage.py your_command email --username foo --password somepwd, note that you need to hash the password.
Van Osem I also have a theoretical question, any help on that will be super appreciated. stackoverflow.com/questions/50536804/…
Also one more question, this creates an user with id : None Why is that happening?
@MelissaStewart: then the database will normally assign a valid id for it.

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.