Skip to content Skip to sidebar Skip to footer

Two Arguments In Django-admin Custom Command

I have a working django-admin custom command that I use to populate my database with new information. Again, everything works. However, I have now changed my models and function sl

Solution 1:

It's entirely possible, although django.core.management does not provide a specific tool to do so. You can parse the arguments passed via the args keyword argument. You'll have to come up with a syntax for doing so (defining the syntax in the help attribute of the command would probably be a good idea).

Assuming the syntax is firstname.lastname, or just firstname in the case that the last name is omitted, you can do something like:

def handle(self, *args, **options):
    for arg in args:
        try:
            first_name, last_name = arg.split('.')
        except ValueError:
            first_name, last_name = arg, None

        info = DEF(first_name, last_name)

And users of the command can pass in arguments like so:

$ python manage.py yourcommand -v=3 john.doe bill patrick.bateman

Post a Comment for "Two Arguments In Django-admin Custom Command"