我想在一个有很多应用程序的项目中找到一个自定义命令,如何从所有应用程序中获得所有命令的列表?
此命令将列出所有已安装应用程序的所有自定义或现有命令:
python manage.py help
您还可以使用django模块加载django命令。
要获得项目中所有自定义Django命令的列表,可以使用from django.core.management import get_commands
。此get_commands
函数从运行的应用程序返回所有可用命令及其相关应用程序的字典。
以下是如何使用此功能显示所有命令及其相关应用程序的示例:
from django.core.management import get_commands
commands = get_commands()
print([command for command in commands.items()])
#sample output
$ [('check', 'django.core'),
('compilemessages', 'django.core'),
('createcachetable', 'django.core'),
('dbshell', 'django.core'),
('diffsettings', 'django.core'),
('dumpdata', 'django.core'),
('flush', 'django.core'),
('inspectdb', 'django.core'),
('loaddata', 'django.core'),
('makemessages', 'commands'),
('makemigrations', 'django_migration_linter'),
('migrate', 'django.core'),
('runserver', 'django.contrib.staticfiles'),
('sendtestemail', 'django.core'),
('shell', 'django.core'),
]
如果只想显示特定应用程序的命令,可以使用以下代码过滤get_commands((的结果:
[command for command in commands.items() if command[1] == 'app_name']
将"app_name"替换为要显示其命令的应用程序的名称。