Flask-Admin can_create =仅对给定用户为True [英] Flask-Admin can_create = True only for a given user

查看:75
本文介绍了Flask-Admin can_create =仅对给定用户为True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Flask-Admin与Flask-Login和mongoengine结合使用.

I am using Flask-Admin in conjunction with Flask-Login and mongoengine.

我希望根据用户自定义视图.此后,带有 can_create 的示例允许创建模型.

I wish to customize views depending on users. Hereafter, an example with can_create, that allows model creation.

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()


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


Such a piece of code has no effect: all users can still create, no difference between admin and non-admin users.

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

Thanks a lot for any hint on how it's possible to allow model creation only to a given user.

推荐答案

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

Look like that you just created local variable can_create, so you can try self.can_create = False. But flask-admin create one instance of View and this can be problems with concurrency. However better separate logic for checking accessibility and changing view state. So probably better use next code:

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'

这篇关于Flask-Admin can_create =仅对给定用户为True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆