The Django management commands documentation shows all commands being created in an app/management/commands folder. Is it possible to put commands into subfolders, like app/management/commands/install and app/management/commands/maintenance? How would this be done?
Django管理命令文档显示了在app / management / commands文件夹中创建的所有命令。是否可以将命令放入子文件夹,例如app / management / commands / install和app / management / commands / maintenance?这怎么办?
1 个解决方案
#1
5
Unfortunatly, as of Django 1.4 there seems to be no way of doing that. The sources for django.core.management.__init__.py
have this method:
不幸的是,从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 []
As you can see, it only considers files directly inside the commands
folder, ignoring any subfolders. However, if you "monkey patch" this function somehow, the rest of the code should work fine, since the code that actually creates the Command
instance is this:
如您所见,它只考虑命令文件夹中的文件,忽略任何子文件夹。但是,如果你以某种方式“修补”这个函数,其余的代码应该可以正常工作,因为实际创建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()
So, if you had a command named subfolder.command
it would load the right script and instantiate the right class.
因此,如果您有一个名为subfolder.command的命令,它将加载正确的脚本并实例化正确的类。
From a practical standpoint, however, I see no use of doing that. Sure, having "namespace'd" commands would be nice, but you can always prefix all your commands with some name if you want, using something else as a separator (such as _
). The command name length - and the number of keystrokes needed to type them in the terminal - will be the same...
然而,从实际的角度来看,我认为没有这样做。当然,拥有“命名空间”命令会很好,但如果你愿意,你总是可以在所有命令前面添加一些名称,使用其他东西作为分隔符(例如_)。命令名称长度 - 以及在终端中键入它们所需的击键次数 - 将是相同的......
#1
5
Unfortunatly, as of Django 1.4 there seems to be no way of doing that. The sources for django.core.management.__init__.py
have this method:
不幸的是,从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 []
As you can see, it only considers files directly inside the commands
folder, ignoring any subfolders. However, if you "monkey patch" this function somehow, the rest of the code should work fine, since the code that actually creates the Command
instance is this:
如您所见,它只考虑命令文件夹中的文件,忽略任何子文件夹。但是,如果你以某种方式“修补”这个函数,其余的代码应该可以正常工作,因为实际创建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()
So, if you had a command named subfolder.command
it would load the right script and instantiate the right class.
因此,如果您有一个名为subfolder.command的命令,它将加载正确的脚本并实例化正确的类。
From a practical standpoint, however, I see no use of doing that. Sure, having "namespace'd" commands would be nice, but you can always prefix all your commands with some name if you want, using something else as a separator (such as _
). The command name length - and the number of keystrokes needed to type them in the terminal - will be the same...
然而,从实际的角度来看,我认为没有这样做。当然,拥有“命名空间”命令会很好,但如果你愿意,你总是可以在所有命令前面添加一些名称,使用其他东西作为分隔符(例如_)。命令名称长度 - 以及在终端中键入它们所需的击键次数 - 将是相同的......