如何将flask -table of flask-sqlalchemy添加到flask-admin?

时间:2016-03-16 07:44:28

标签: flask flask-sqlalchemy flask-admin

flask-securityflask-admin集成后,flask-security需要创建一个名为roles_usersdb.Tableflask-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.Tabledb.Model

之间的关系

我想知道的是如何将roles_users注册到flask-admin

1 个答案:

答案 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}