我有以下类,名为 custom_class.py 。我需要的是一种检查我的观点的方法如果某个用户是管理员,显示某些DIV ,如果没有,请隐藏它们。任何帮助表示赞赏。
class Backend_user:
parents = {'Administrator': 'None',
'Supervisor': 'Administrator',
'Operator': 'Supervisor',
}
childs = {'Administrator': 'Supervisor',
'Supervisor': 'Operator',
'Operator': 'Boss',
}
def __init__(self, user):
self.user = user
self.permissions = OrderedDict()
self.permissions_plural = OrderedDict()
self.permissions_plural['Administrator'] = 'Administrators'
self.permissions_plural['Supervisor'] = 'Supervisors'
if hasattr(user, "administrator"):
self.my_specialty = getattr(user, 'administrator')
self.permissions['Administrator'] = True
self.permissions['Supervisor'] = True
self.permissions['Operator'] = True
self.permissions['Boss'] = True
elif hasattr(user, "supervisor"):
self.my_specialty = getattr(user, 'supervisor')
self.permissions['Administrator'] = False
self.permissions['Supervisor'] = True
self.permissions['Operator'] = True
self.permissions['Boss'] = True
答案 0 :(得分:0)
假设您的用户表具有某个名称,该用户表也是Backend_user表的ForeignKey,因此您只需检查用户是否为admin。您必须从用户表中获取用户对象,然后检查它是否具有该属性
def some_view(request):
user_obj = CustomUser.objects.get(id=some_id) # get the user here
if hasattr(user_obj, "administrator"): # checking if user is admin
# show some div
else:
# don't show