Opened 16 months ago

Closed 16 months ago

Last modified 16 months ago

#30305 closed Uncategorized (invalid)

Feature to set the returncode of a django custom command

Reported by: stephanm Owned by: nobody
Component: Uncategorized Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


In the docs:

I can see that the handle() function will return a string which goes to stdout.

But I would like to set (supplementary) an integer returncode like its done in sys.exit() see

I didn't find any "official" way to do this, perhaps this is a new feature.


Change History (3)

comment:1 Changed 16 months ago by felixxm

Resolution: invalid
Status: newclosed
Version: 2.1master

Thanks for the report, however answer for your question is already in the ticket description, i.e. you can use sys.exit(custom_number) in the handle().

comment:2 Changed 16 months ago by stephanm

Resolution: invalid
Status: closednew


I stepped a little through the code after the handle() function returns.

Here I see in that I pass through the function
which itself calls a connections.close_all() function at the end,
so using sys.exit() inside the handle() function will not give the exact same
behaviour as a "controlled" exit.

Or do I miss something?

See: run_from_argv(self, argv)

def run_from_argv(self, argv):
        self.execute(*args, **cmd_options)
    except Exception as e:
        except ImproperlyConfigured:
            # Ignore if connections aren't setup at this point (e.g. no
            # configured settings).

comment:4 Changed 16 months ago by felixxm

Resolution: invalid
Status: newclosed

sys.exit() works fine, e.g.

class Command(BaseCommand):
    def handle(self, *args, **options):
> python test_command
> echo $?

All cleanup actions are honored (see sys.exit documentation). Please use one of support channels if you have more questions.

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