加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Flask-Admin can_create =仅对给定用户为True

发布时间:2020-12-20 11:27:53 所属栏目:Python 来源:网络整理
导读:我将Flask-Admin与Flask-Login和mongoengine结合使用. 我希望根据用户自定义视图.以下是can_create的示例,它允许模型创建. class MyModelView(ModelView): column_exclude_list = ['password'] def is_accessible(self): if (login.current_user.login != 'a
我将Flask-Admin与Flask-Login和mongoengine结合使用.

我希望根据用户自定义视图.以下是can_create的示例,它允许模型创建.

class MyModelView(ModelView):
    column_exclude_list = ['password']
    def is_accessible(self):
        if (login.current_user.login != 'admin'):
            can_create=False
        return login.current_user.is_authenticated()

这样的代码没有任何效果:所有用户仍然可以创建,管理员和非管理员用户之间没有区别.

非常感谢任何关于如何仅允许给定用户创建模型的提示.

解决方法

看起来你刚刚创建了局部变量can_create,所以你可以尝试self.can_create = False.但flask-admin创建一个View实例,这可能是并发问题.然而,更好的单独逻辑用于检查可访问性和更改视图状态.所以最好使用下一个代码:

class MyModelView(ModelView):
    column_exclude_list = ['password']

    def is_accessible(self):
        return login.current_user.is_authenticated()

    @property
    def can_create(self):
        return login.current_user.login == 'admin'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读