#10080 closed Bug (fixed)
call_command doesn't take into account command's default options
Reported by: | Alexander Koshelev | Owned by: | Alexander Koshelev |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When I want to use manage.py command from python I must specify all options on which this command is depend.
Almost everyone custom command's author write execute
method something like this:
class Command(BaseCommand): option_list = [make_option("--some_option", default="foobar")] def execute(self, *args, **options): #... some_option = options["some_option"] #...
And when I call it using call_command
without some_option
is specified I get the KeyError
. But it works fine in command line because optparse
provides default values.
>>> call_command("some_command") Traceback (most recent call last): ... KeyError: 'some_option'
Patch solves this inconsistence.
Attachments (2)
Change History (12)
by , 16 years ago
Attachment: | 10080.diff added |
---|
comment:2 by , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:4 by , 14 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
The patch solves the problem if the option has a default value, it still doesn't work if no default value is specified. In those cases, optparse sets the options value to None but call_command plainly excludes it which leads to problems with some commands. This update makes it work for me:
defaults = {} for o in klass.option_list: v = o.default if v is NO_DEFAULT: v = None defaults[o.dest] = v
comment:5 by , 14 years ago
milestone: | 1.1 |
---|
comment:6 by , 14 years ago
Needs tests: | set |
---|---|
Severity: | → Normal |
Type: | → Bug |
by , 13 years ago
Attachment: | 10080-2.diff added |
---|
Also fill defaults dict with options without defaults
comment:7 by , 13 years ago
Easy pickings: | unset |
---|---|
Needs tests: | unset |
UI/UX: | unset |
comment:8 by , 13 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Patch