-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Problem
Currently, Flask-Admin registers each view's blueprint directly to the Flask app instance, which can lead to blueprint name collisions (e.g. #55, #910) and makes it harder to manage large admin interfaces. There is currently no hierarchical grouping of admin-related routes, which I think can feel disorganized in complex applications.
Proposed Solution
I propose modifying Flask-Admin to optionally register all view blueprints as nested blueprints under the AdminIndexView's blueprint (named admin by default) or other one created initially. This leverages Flask's nested blueprint feature (Flask 2.0+) to:
- Group admin routes under a single parent blueprint, like
/admin/users/and/admin/roles/ - Reduce namespace collisions by scoping view blueprints within the admin namespace
- Improve modularity and maintainability for large applications
Example Implementation
# base.py
class Admin:
def __init__(self, app=None, name=None, url='/admin', index_view=None, use_nested_blueprints=False):
self.use_nested_blueprints = use_nested_blueprints
self.index_blueprint = self.index_view.create_blueprint(self)
if app is not None:
self.init_app(app)
def add_view(self, view):
self.views.append(view)
blueprint = view.create_blueprint(self)
if self.use_nested_blueprints:
self.index_blueprint.register_blueprint(blueprint)
else:
self.app.register_blueprint(blueprint)Although the parameter use_nested_blueprints was not in my current plan, I wanted to describe it and thought maybe it has to be there for backward compatibility.
I will be happy if anyone can leave your opinions and suggestions!