Search code examples
pythonflaskflask-admin

Flask-Admin: A blueprint's name collision occurred ... Both share the same name "admin"


I have a Flask 0.12.4 app and am using Flask-Admin.

I get this error intermittently when developing locally, and have just started seeing it in one of our public environments too.

AssertionError: A blueprint's name collision occurred between 
<flask.blueprints.Blueprint object at 0x7f5cd31f96d0> and 
<flask.blueprints.Blueprint object at 0x7f5cd33b0d90>.
Both share the same name "admin".
Blueprints that are created on the fly need unique names.

The steps to reproduce in my development environment are slightly odd:

  1. Break some of the admin config (e.g. change one of the strings relating to a column name in form_columns so it refers to a non-existent name)
  2. Refresh the browser to see the regular "unknown field" error
  3. Revert the change
  4. Refresh the browser again - you then see the blueprint error above

The line which is causing the error is this one:

# admin.py
admin = flask_admin.Admin(
    app, index_view=MyIndexView(), template_mode="bootstrap3"
)

Solution

  • It seemed like the line in question was being called multiple times (although the file it's in is only imported in one place). I tried passing a custom endpoint in the Admin constructor and continued to get the same error but with the new endpoint name, which suggested that was the case.

    This answer refers in passing to the same problem, and to the solution of using init_app rather than passing the app to the constructor.

    Doing that, my code then looked like this, and the error went away:

    # admin.py
    # Notice I'm not passing app here any more.
    admin = flask_admin.Admin(index_view=MyIndexView(), template_mode="bootstrap3")
    
    # app.py
    admin.init_app(app) # This line is new
    

    I don't understand the detail of how the problem occurred or exactly why this solved it, so any further pointers welcome!