Skip to main content
 首页 » 编程设计

python之如何在 alembic 初始化期间使用自定义模板

2024年11月01日8myhome

根据文档,alembic 的模板可以指定为 alembic init --template pylons ./scripts。但是 alembic 仅在其根目录中的一个文件夹中搜索模板 (python_path/lib/python2.7/site-packages/alembic/templates)。

有什么方法可以运行自定义模板吗?问题的想法 - 项目设置的自动化,例如避免任何手工更改(如编辑 env.py 文件)。

请您参考如下方法:

正如我在 Alembic 源代码中看到的那样,无法自定义模板目录位置。模板目录由 Config.get_template_directory() 获取并被 list_templates() 使用.

虽然你可以尝试使用monkey patch :

from alembic.config import Config 
 
def get_template_directory(self): 
    return "my/custom/template/directory" 
 
Config.get_template_directory = get_template_directory 
 
# Invoke Alembic code 

附言我想知道为什么 Alembic 不使用入口点来收集可用模板。