Reputation: 2568
The time has come where I need to release a new version for my api and still support the previous one.
I followed the instructions given in the accepted answer for this question. Unfortunately I don't have enough rep to ask in the comments of that answer.
I have an app structure that looks like this:
+-- app/
+-- api_2_0/
+-- __init__.py
(...)
+-- api_2_1/
+-- __init__.py
(...)
+-- __init__.py
Both are blueprints which I initialize this way in my __init__.py
in the create_app
method (I'm using the app factory method):
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')
But this causes:
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.
It's true that both are called api
inside their folders but I'm importing them with different names. Having to rename all the calls to api
to something else for each version would make versioning painful and overall a code mess.
Is there a better way to do this?
Upvotes: 2
Views: 1125
Reputation: 2568
Ok so it turns out I only had to change the Blueprint definition.
Before I had both blueprints defined as api = Blueprint('api', __name__)
which caused the collision and made me think that I needed to change name of the variable api
.
Turns out what I really needed to change is just the string 'api'
used in the call to Blueprint
, so now my blueprints are defined as api = Blueprint('api_2_0', __name__)
and api = Blueprint('api_2_1', __name__)
, allowing me to keep the variable api
the same in both cases and saving me the issue of chaging it everywhere.
I now realize this was a pretty silly question but I'm leaving this here in case someone runs with the same issue.
Upvotes: 4