将flask-security
与flask-admin
集成后,flask-security
需要创建一个名为roles_users
且db.Table
为flask-sqlalchemy
的表格:
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
这些基于db.Model
flask-sqlalchemy
的课程可以注册到flask-admin
,如下所示:
admin.add_view(ModelView(User, db.session))
我查看了flask-sqlalchemy
的来源,无法弄清楚db.Table
和db.Model
我想知道的是如何将roles_users
注册到flask-admin
。
答案 0 :(得分:0)
通常,您不需要添加用户角色联结表。将用户和角色添加为单独的视图,但在“用户”视图中包括角色关系。例如:
给出以下两个模型和表格:
Vector
使用这两个视图(Flask-Admin 1.4):
{-# LANGUAGE OverloadedStrings, TransformListComp, RankNTypes,
TypeSynonymInstances, FlexibleInstances, OverloadedLists, DeriveGeneric #-}
{-# LANGUAGE MultiParamTypeClasses, FlexibleContexts,
TypeFamilies, ScopedTypeVariables, InstanceSigs #-}
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Unboxed as U
data Column v a = Column {rawVector :: G.Vector v a => v a, colName :: Text, colType :: SupportedTypes }
instance (G.Vector v a, Binary (v a)) => Binary (Column v a) where
put Column {rawVector = vec, colName = cn, colType = ct} = do put (fromEnum ct) >> put cn >> put vec
get = do t <- get :: Get Int
nm <- get :: Get Text
let pt = toEnum t :: SupportedTypes
case pt of
PInt -> do vec <- get :: Get (U.Vector Int64)
return Column {rawVector = vec, colName = nm, colType = pt}
PDouble -> do vec <- get :: Get (U.Vector Double)
return Column {rawVector = vec, colName = nm, colType = pt}