Opened 8 years ago

Closed 8 years ago

#5222 closed (fixed)

Ability for end users to register commands with management.py.

Reported by: dnordberg@… Owned by: adrian
Component: Core (Other) Version: master
Severity: Keywords: management commands
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Changeset [5923] added the ability for end users to register commands with management.py. This was rolled out [5923]-[5925] due to breaking call_command().

I've patched init.py to allow users to register commands with management.py without breaking call_command.

A bit of a hack, but it works. Basically, I set a global dict _DJANGO_COMMANDS to hold all default commands and update itself with project specific commands when DJANGO_SETTINGS_MODULE is set.

call_command changes to:

 def call_command(name, *args, **options):
     """
@@ -26,7 +69,7 @@
         call_command('shell', plain=True)
         call_command('sqlall', 'myapp')
     """
-    klass = load_command_class(name)
+    klass = getattr(__import__(_DJANGO_COMMANDS[name].__module__, {}, {}, ['Command']), 'Command')()

Attachments (4)

__init__.py.diff (4.6 KB) - added by dnordberg@… 8 years ago.
Diff file of init.py allowing end users to register management commands.
__init__.py (9.3 KB) - added by dnordberg@… 8 years ago.
The patched version of init.py
__init__.2.py (7.7 KB) - added by dnordberg@… 8 years ago.
The patched version of init.py with the last line removed.
__init__.py.2.diff (10.4 KB) - added by dnordberg@… 8 years ago.
Diff file of init.py with last line removed.

Download all attachments as: .zip

Change History (8)

Changed 8 years ago by dnordberg@…

Diff file of init.py allowing end users to register management commands.

Changed 8 years ago by dnordberg@…

The patched version of init.py

comment:1 Changed 8 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

The last line was causing problems with runserver so I removed it and another unnecessary line.

This means that using this patch, project commands wont be available for and production server setup or any other time you manually specify DJANGO_SETTINGS_MODULE.

Hence, if you are calling project commands through any views, you should do management._DJANGO_COMMANDS.update(management.load_project_commands()).

Changed 8 years ago by dnordberg@…

The patched version of init.py with the last line removed.

Changed 8 years ago by dnordberg@…

Diff file of init.py with last line removed.

comment:2 Changed 8 years ago by anonymous

  • Patch needs improvement set

comment:3 Changed 8 years ago by anonymous

  • Patch needs improvement unset

comment:4 Changed 8 years ago by russellm

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

(In [6047]) Fixed #5212, #5222 -- Added the ability for users to register their own commands with django-admin. A previous attempt at this was introduced in [5923]-[5925], and rolled out in [5929].

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