I'm trying to use Django's call_command
in a manner very similar to this question without an answer.
The way I'm calling it is:
args = []
kwargs = {
'solr_url': 'http://127.0.0.1:8983/solr/collection1',
'type': 'opinions',
'update': True,
'everything': True,
'do_commit': True,
'traceback': True,
}
call_command('cl_update_index', **kwargs)
In theory, that should work, according to the docs. But it doesn't work, it just doesn't.
Here's the add_arguments
method for my Command class:
def add_arguments(self, parser):
parser.add_argument(
'--type',
type=valid_obj_type,
required=True,
help='Because the Solr indexes are loosely bound to the database, '
'commands require that the correct model is provided in this '
'argument. Current choices are "audio" or "opinions".'
)
parser.add_argument(
'--solr-url',
required=True,
type=str,
help='When swapping cores, it can be valuable to use a temporary '
'Solr URL, overriding the default value that\'s in the '
'settings, e.g., http://127.0.0.1:8983/solr/swap_core'
)
actions_group = parser.add_mutually_exclusive_group()
actions_group.add_argument(
'--update',
action='store_true',
default=False,
help='Run the command in update mode. Use this to add or update '
'items.'
)
actions_group.add_argument(
'--delete',
action='store_true',
default=False,
help='Run the command in delete mode. Use this to remove items '
'from the index. Note that this will not delete items from '
'the index that do not continue to exist in the database.'
)
parser.add_argument(
'--optimize',
action='store_true',
default=False,
help='Run the optimize command against the current index after '
'any updates or deletions are completed.'
)
parser.add_argument(
'--do-commit',
action='store_true',
default=False,
help='Performs a simple commit and nothing more.'
)
act_upon_group = parser.add_mutually_exclusive_group()
act_upon_group.add_argument(
'--everything',
action='store_true',
default=False,
help='Take action on everything in the database',
)
act_upon_group.add_argument(
'--query',
help='Take action on items fulfilling a query. Queries should be '
'formatted as Python dicts such as: "{\'court_id\':\'haw\'}"'
)
act_upon_group.add_argument(
'--items',
type=int,
nargs='*',
help='Take action on a list of items using a single '
'Celery task'
)
act_upon_group.add_argument(
'--datetime',
type=valid_date_time,
help='Take action on items newer than a date (YYYY-MM-DD) or a '
'date and time (YYYY-MM-DD HH:MM:SS)'
)
No matter what I do here, I get:
CommandError: Error: argument --type is required
Any ideas? If you're truly curious, you can see the entire code here.
You defined an argument with a
'--type'
flag, and made itrequired
. That command line will require a string or strings that look like--type avalue
.This looks like the relevant part of
call_command
:It creates a parser, using it's own arguments plus the ones you add.
parser._actions if s_opt.option_strings
are the arguments (Actions) that take an option flag (start with - or --).opt_mapping
is map between the flag strings (minus the leading -s) and the 'dest' attribute.arg_options
converts your**kwargs
to something that can be merged with theparser
output.defaults = parser.parse_args(args=args)
does the actual parsing. That is, it's the only code that actually uses theargparse
parsing mechanism. So the*args
part of your call simulates generatingsys.argv[1:]
from an interactive call.Based on that reading I think this should work:
Instead of
**kwargs
I am passing in values as a list of strings. Or the two required arguments could be passed inargs
, and the rest in**kwargs
.If an argument is
required
it needs to passed in through*args
.**kwargs
bypass the parser, causing it to object about missing arguments.I've downloaded the latest
django
, but haven't installed it. But here's a simulation ofcall_command
that should test the calling options:which produces:
With a bunch of stubs, I can make your
cl
Command
work with myBaseCommand
, and the following call works:performing a stub
everything
.