简体   繁体   English

自定义Django命令的自定义位置

[英]Custom location for custom Django commands

In my Django project I have multiple apps and would like to add custom Command which is general ( populate_db ) to all apps. 在我的Django项目中,我有多个应用程序,并希望向所有应用程序添加通用的自定义Commandpopulate_db )。 However, Django seems to be registering custom commands only from the INSTALLED_APPS locations (ie you must place management folder within a folder of an app). 但是,Django似乎仅从INSTALLED_APPS位置注册自定义命令(即,您必须将management文件夹放置在应用程序的文件夹中)。

Is there a way to place management folder in project_folder . 有没有办法将management文件夹放置在project_folder Here is the desired dir structure: 这是所需的目录结构:

.
├── __init__.py
├── app_1
|   ├── admin.py
|   └── ...
├── app_2
|   ├── admin.py
|   └── ...
├── project_folder
|   ├── settings.py
|   └── management
|       ├── __init__.py
|       └── commands
|           ├── __init__.py
|           └── populate_db.py    
└── manage.py

Django only looks for management commands in management/commands for your installed apps. Django仅在已安装应用程序的management/commands查找管理命令。 There isn't another way to register them. 没有其他注册方式。

In your case, you could add project_folder to your INSTALLED_APPS , and Django should find your management command. 在您的情况下,可以将project_folder添加到INSTALLED_APPS ,并且Django应该找到您的管理命令。

Another common approach is to create an app called 'core' or 'main' , which includes utilities which are for the entire project like your populate_db management command. 另一种常见的方法是创建一个名为'core''main'的应用程序,其中包括适用于整个项目的实用程序,例如populate_db管理命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM