Two arguments in django-admin custom command

后端 未结 1 1509
伪装坚强ぢ
伪装坚强ぢ 2021-01-04 22:17

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 c

相关标签:
1条回答
  • 2021-01-04 22:28

    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
    
    0 讨论(0)
提交回复
热议问题