【发布时间】:2016-07-28 05:00:22
【问题描述】:
我需要为我的 api 发布一个新版本并仍然支持以前的版本的时候到了。
我按照this question 的已接受答案中给出的说明进行操作。不幸的是,我没有足够的代表在该答案的 cmets 中提问。
我的应用结构如下所示:
+-- app/
+-- api_2_0/
+-- __init__.py
(...)
+-- api_2_1/
+-- __init__.py
(...)
+-- __init__.py
两者都是我在__init__.py 中的create_app 方法中以这种方式初始化的蓝图(我正在使用app factory 方法):
def create_app(config_name):
app = Flask(__name__)
(...)
from .api_2_0 import api as api_2_0_blueprint
app.register_blueprint(api_2_0_blueprint, url_prefix='/api/v2.0')
from .api_2_1 import api as api_2_1_blueprint
app.register_blueprint(api_2_1_blueprint, url_prefix='/api/v2.1')
但这会导致:
AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x7f8e48e82c10> and <flask.blueprints.Blueprint object at 0x7f8e48ef7150>. Both share the same name "api". Blueprints that are created on the fly need unique names.
确实,两者在它们的文件夹中都被称为api,但我正在以不同的名称导入它们。必须将每个版本的所有对 api 的调用重命名为其他名称,这会使版本控制变得痛苦,并且总体上会造成代码混乱。
有没有更好的方法来做到这一点?
【问题讨论】:
-
你好,你能告诉我api版本文件夹中的init.py文件的目的是什么吗?
-
@variable 一个
__init__.py文件告诉python它们所在的文件夹是一个包。在此处查看文档:docs.python.org/3/reference/import.html#regular-packages
标签: python flask versioning