Django call_command error with missing required arguments

I want to call the Django management command from one of my tests. For this I use django.core.management.call_command . And it does not work.

I have a team with 4 required arguments. When I call it, he complains that all arguments are missing, although I pass them on:

 call_command('my_command', url='12', project='abc', website='zbb', title='12345') 

I get a basic command error that -url, -project, -website and -title objects are missing.

I did not specify a different destination for these arguments.

I looked at the source of call_command and pointed the problem to the following line in call_command :

 if command.use_argparse: # Use the `dest` option name from the parser option opt_mapping = {sorted(s_opt.option_strings)[0].lstrip('-').replace('-', '_'): s_opt.dest for s_opt in parser._actions if s_opt.option_strings} arg_options = {opt_mapping.get(key, key): value for key, value in options.items()} defaults = parser.parse_args(args=args) ****** THIS ***** defaults = dict(defaults._get_kwargs(), **arg_options) # Move positional args out of options to mimic legacy optparse args = defaults.pop('args', ()) 

args are positional arguments passed to call_commands, which is empty. I pass only the arguments. parser.parse_args complains that the required variables are missing.

This is in Django 1.8.3.

Here is my add_arguments function command (I just deleted the help lines for short):

 def add_arguments(self, parser): parser.add_argument('--url', action='store', required=True) parser.add_argument('--project', action='store', required=True) parser.add_argument('--continue-processing', action='store_true', default=False) parser.add_argument('--website', action='store', required=True) parser.add_argument('--title', action='store', required=True) parser.add_argument('--duplicate', action='store_true',default=False) 
+5
source share
1 answer

Based on this piece of code that you posted, I wrapped the call_command argument

that the required named arguments must be passed through *args , not just positional.

**kwargs bypasses the parser. Therefore, he does not see anything that you have identified there. **kwargs can override *args values, but *args still needs something for each argument required. If you do not want to do this, disable the required attribute.

+3
source

All Articles