在flask security中为users.id提供自定义主键

在flask security中为users.id提供自定义主键,flask,flask-login,flask-security,flask-principal,Flask,Flask Login,Flask Security,Flask Principal,我想为字段id设置一个自定义键,例如id\u user,我尝试了以下方法 class UserModel(db.model, UserMixin) ... @property def id(self): return self.id_user 但无法使它工作。当我尝试登录时,它会向我发送以下消息: { "message": "You don't have the permission to access the requested res

我想为字段id设置一个自定义键,例如id\u user,我尝试了以下方法

class UserModel(db.model, UserMixin)
...
        @property
        def id(self):
            return self.id_user
但无法使它工作。当我尝试登录时,它会向我发送以下消息:

{
"message": "You don't have the permission to access the requested resource. It is either read-protected or not readable by the server."
}

我最终得到了一个糟糕的解决方案。我克隆了UserModel对象,为id添加了一个重复的字段,使用了我需要的自定义密钥,并告诉Flask Security将该对象用作UserModel这是我使用的函数代码:

def clone_model(model):
data = model
attr = getattr(model, "id_user")
setattr(data, "id", attr)
return data

cUserModel = clone_model(UserModel)
user_datastore = SQLAlchemyUserDatastore(db, cUserModel, Roles)

security = Security(app, user_datastore)

希望有人觉得它有用

需要更多信息-您使用的是什么版本的flask security和flask。我认为这个错误消息并不是来自Flask Security——如果登录失败——假设您使用的是表单——您应该(从POST/login)获得一个表单。