我如何注册蓝图并将该应用程序添加到flask-admin

时间:2015-07-01 06:36:35

标签: python flask flask-admin blueprint

我的代码:

__初始化__。PY

from flask import Flask
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

from user.user import mod as user
from user.models import User as userModel

app.register_blueprint(user, url_prefix='/user')

admin = Admin(app, name='My app')
admin.add_view(ModelView(userModel, db.session, name='userAdmin'))

user.py:

from flask import Blueprint, json
from flask.views import MethodView

mod = Blueprint('user', __name__)

class UserAPI(MethodView):
    def get(self):
        users = [
            {'nickname': 'Chan'},
            {'nickname': 'Hzz'},
        ]
        return json.dumps(users)

mod.add_url_rule('/users/', view_func=UserAPI.as_view('users'))

models.py:

from app import db

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)

    def __init__(self, username, email):
        self.username = username
        self.email = email

    def __repr__(self):
        return "<User %s>" % self.username

我的用户应用程序中有蓝图,我已经注册了,但是当我想将此添加到我的管理员来管理用户数据时,它会抛出以下异常:

Traceback (most recent call last):
  File "run.py", line 2, in <module>
    from app import app
  File "/home/chenhj/flask/multiapp/app/__init__.py", line 21, in <module>
    admin.add_view(ModelView(userModel, db.session, name='chj'))
  File "/home/chenhj/.virtualenvs/multiapp/local/lib/python2.7/site-packages/flask_admin/base.py", line 526,     in add_view
    self.app.register_blueprint(view.create_blueprint(self))
  File "/home/chenhj/.virtualenvs/multiapp/local/lib/python2.7/site-packages/flask/app.py", line 62, in     wrapper_func
    return f(self, *args, **kwargs)
  File "/home/chenhj/.virtualenvs/multiapp/local/lib/python2.7/site-packages/flask/app.py", line 885, in     register_blueprint
    (blueprint, self.blueprints[blueprint.name], blueprint.name)
AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x25e5d90> and <flask.blueprints.Blueprint object at 0x21b89d0>.  Both share the same name "user".  Blueprints that are created on the fly need unique names.

我为此疯狂

3 个答案:

答案 0 :(得分:6)

如果您的蓝图已经有了用户名,那么您的管理员就可以了。您的管理员用户模型视图的蓝图需要被称为不同的东西 您可以使用ModelView中的端点var实现此目的 Flask-Admin - ModelView

admin.add_view(ModelView(Users, db.session, endpoint="users_"))

答案 1 :(得分:2)

碰撞是因为您有一个模块名称用户和一个名为user的蓝图。将蓝图重命名为user_blueprint。从代码中可以看出,您有一个名为user的文件夹,一个名为user的模块和一个名为user的蓝图。稍后您可以使用一些描述性名称来避免问题。否则,这简直令人困惑。

答案 2 :(得分:1)

您还可以覆盖ModelView子类中的管理蓝图名称:

from flask_admin.contrib.sqla import ModelView

class AppModelView(ModelView):
    def create_blueprint(self, admin):
        blueprint = super(AppModelView, self).create_blueprint(admin)
        blueprint.name = '{}_admin'.format(blueprint.name)
        return blueprint

    def get_url(self, endpoint, **kwargs):
        if not (endpoint.startswith('.') or endpoint.startswith('admin.')):
            endpoint = endpoint.replace('.', '_admin.')
        return super(AppModelView, self).get_url(endpoint, **kwargs)