Opened 9 years ago

Closed 6 years ago

Last modified 6 years ago

#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: master
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


When I want to use 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)

10080.diff (2.4 KB) - added by Alexander Koshelev 9 years ago.
10080-2.diff (1.6 KB) - added by Claude Paroz 7 years ago.
Also fill defaults dict with options without defaults

Download all attachments as: .zip

Change History (12)

Changed 9 years ago by Alexander Koshelev

Attachment: 10080.diff added


comment:1 Changed 9 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:2 Changed 9 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 Changed 9 years ago by Jacob

Resolution: fixed
Status: newclosed

(In [10401]) [1.0.X] Fixed #10080: call_command now takes option defaults into account, sparing individual commands from any difference between call_command and being run from the shell. Thanks, Alex Koshelev. Backport of 10400 from trunk.

comment:4 Changed 8 years ago by bjourne

Resolution: fixed
Status: closedreopened

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 Changed 8 years ago by anonymous

milestone: 1.1

comment:6 Changed 7 years ago by Chris Beaven

Needs tests: set
Severity: Normal
Type: Bug

Changed 7 years ago by Claude Paroz

Attachment: 10080-2.diff added

Also fill defaults dict with options without defaults

comment:7 Changed 7 years ago by Claude Paroz

Easy pickings: unset
Needs tests: unset
UI/UX: unset

comment:8 Changed 6 years ago by Jannis Leidel

Triage Stage: AcceptedReady for checkin

comment:9 Changed 6 years ago by Jannis Leidel

Resolution: fixed
Status: reopenedclosed

In [17467]:

(The changeset message doesn't reference this ticket)

comment:10 Changed 6 years ago by Ramiro Morales

In [17678]:

Fixed #17820 -- Fixed more occurrences of redundant handling of management commands options.

They had been missen in the first attempts or had been introduced afterwards.

Refs #13760, #10080, #17799.

Note: See TracTickets for help on using tickets.
Back to Top