Opened 6 years ago

Closed 3 years ago

Last modified 3 years ago

#10080 closed Bug (fixed)

call_command doesn't take into account command's default options

Reported by: alexkoshelev Owned by: alexkoshelev
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

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)

10080.diff (2.4 KB) - added by alexkoshelev 6 years ago.
Patch
10080-2.diff (1.6 KB) - added by claudep 3 years ago.
Also fill defaults dict with options without defaults

Download all attachments as: .zip

Change History (12)

Changed 6 years ago by alexkoshelev

Patch

comment:1 Changed 6 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:2 Changed 6 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

(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 5 years ago by bjourne

  • Resolution fixed deleted
  • Status changed from closed to 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 Changed 4 years ago by anonymous

  • milestone 1.1 deleted

comment:6 Changed 4 years ago by SmileyChris

  • Needs tests set
  • Severity set to Normal
  • Type set to Bug

Changed 3 years ago by claudep

Also fill defaults dict with options without defaults

comment:7 Changed 3 years ago by claudep

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

comment:8 Changed 3 years ago by jezdez

  • Triage Stage changed from Accepted to Ready for checkin

comment:9 Changed 3 years ago by jezdez

  • Resolution set to fixed
  • Status changed from reopened to closed

In [17467]:

(The changeset message doesn't reference this ticket)

comment:10 Changed 3 years ago by ramiro

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