Oll*_*ass 9 django django-manage.py
Django管理命令文档显示了在app/management/commands文件夹中创建的所有命令.是否可以将命令放入子文件夹,例如app/management/commands/install和app/management/commands/maintenance?怎么做?
不幸的是,从Django 1.4开始,似乎没有办法做到这一点.django.core.management.__init__.py有这种方法的来源:
def find_commands(management_dir):
"""
Given a path to a management directory, returns a list of all the command
names that are available.
Returns an empty list if no commands are defined.
"""
command_dir = os.path.join(management_dir, 'commands')
try:
return [f[:-3] for f in os.listdir(command_dir)
if not f.startswith('_') and f.endswith('.py')]
except OSError:
return []
Run Code Online (Sandbox Code Playgroud)
如您所见,它只考虑文件commands夹内的文件,忽略任何子文件夹.但是,如果您以某种方式"修补"此函数,其余代码应该可以正常工作,因为实际创建Command实例的代码是这样的:
def load_command_class(app_name, name):
"""
Given a command name and an application name, returns the Command
class instance. All errors raised by the import process
(ImportError, AttributeError) are allowed to propagate.
"""
module = import_module('%s.management.commands.%s' % (app_name, name))
return module.Command()
Run Code Online (Sandbox Code Playgroud)
因此,如果你有一个名为的命令subfolder.command,它将加载正确的脚本并实例化正确的类.
然而,从实际的角度来看,我认为没有这样做.当然,拥有"命名空间"命令会很好,但如果你愿意,你总是可以在所有命令前加上一些名字,使用其他东西作为分隔符(例如_).命令名称长度 - 以及在终端中键入它们所需的击键次数 - 将是相同的......
| 归档时间: |
|
| 查看次数: |
1290 次 |
| 最近记录: |