gpt4 book ai didi

Flask-Admin can_create = True 仅适用于给定用户

转载 作者:行者123 更新时间:2023-12-03 15:57:06 25 4
gpt4 key购买 nike

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

我希望根据用户自定义 View 。下面是一个带有 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 实例,这可能是并发问题。然而,更好的独立逻辑用于检查可访问性和更改 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'

关于Flask-Admin can_create = True 仅适用于给定用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17137601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com