In my django admin section, I'd like to show different versions of the admin page depending on what kind of user is currently logged in. I can think of a couple ways this might work, but haven't figured out how to do any of them.
在我的django管理部分中,我希望显示管理页面的不同版本,这取决于当前登录的用户类型。我可以想出一些方法,但是还没有想出如何去做。
Perhaps I could put logic into the admin.ModelAdmin to look at the current user and change the 'exclude' field dynamically. Does that work? Or maybe run different custom templates based on who's logged in, and have the templates include / exclude the fields as appropriate.
也许我可以把逻辑输入管理员。ModelAdmin查看当前用户并动态更改“排除”字段。这工作吗?或者根据登录的用户运行不同的自定义模板,并让模板包含/排除适当的字段。
I could register two versions of the admin.ModelAdmin class, one for each type of user, and maybe restrict access through permissions? But the permissions system seems to believe fairly deeply in one set of permissions per model class so I'm not sure how to change that.
我可以注册两个版本的admin。ModelAdmin类,针对每种类型的用户,并可能通过权限限制访问?但是权限系统似乎非常信任每个模型类的一组权限,所以我不知道如何更改它。
I could grab a couple of the widgets that are used in rendering the admin page templates, and include them in my own page that does the one specific job I need powerful users to be able to do.
我可以获取一些用于呈现管理页面模板的小部件,并将它们包含在我自己的页面中,以完成我需要强大用户才能完成的特定工作。
I could set up multiple AdminSites and restrict access to them through the url / view system. But then I'm not sure how to register different admin.ModelAdmin classes with the different AdminSites.
我可以设置多个adminsite,并通过url / view系统限制对它们的访问。但是我不知道如何注册不同的管理员。使用不同管理员的ModelAdmin类。
Any advice on this would be appreciated.
如对此有任何建议,我们将不胜感激。
Answer
回答
Thanks for the hint. Here's how I did it...
谢谢你的提示。我是这样做的……
def get_form(self, request, obj=None, **kwargs):
"""This dynamically inserts the "owners" field into the exclude list
if the current user is not superuser.
"""
if not request.user.is_superuser:
if self.exclude:
self.exclude.append('owners')
else:
self.exclude = ['owners']
else:
# Necessary since Admin objects outlive requests
try:
self.exclude.remove('owners')
except:
pass
return super(OwnersModelAdmin,self).get_form(request, obj=None, **kwargs)
1 个解决方案
#1
1
There are quite a few hooks provided in the ModelAdmin
class for this sort of thing.
在ModelAdmin类中提供了一些类似的钩子。
One possibility would be to override the get_form
method. This takes the request, as well as the object being edited, so you could get the current user from there, and return different ModelForms dependent on the user.
一种可能是重写get_form方法。它接受请求和正在编辑的对象,因此您可以从那里获得当前用户,并返回依赖于用户的不同的ModelForms。
It's worth looking at the source for ModelAdmin
- it's in django.contrib.admin.options - to see if overriding this or any other other methods might meet your needs.
它值得查看ModelAdmin的源代码——它位于django.后悔管理中。选项-查看是否覆盖此方法或其他任何方法可能满足您的需求。
#1
1
There are quite a few hooks provided in the ModelAdmin
class for this sort of thing.
在ModelAdmin类中提供了一些类似的钩子。
One possibility would be to override the get_form
method. This takes the request, as well as the object being edited, so you could get the current user from there, and return different ModelForms dependent on the user.
一种可能是重写get_form方法。它接受请求和正在编辑的对象,因此您可以从那里获得当前用户,并返回依赖于用户的不同的ModelForms。
It's worth looking at the source for ModelAdmin
- it's in django.contrib.admin.options - to see if overriding this or any other other methods might meet your needs.
它值得查看ModelAdmin的源代码——它位于django.后悔管理中。选项-查看是否覆盖此方法或其他任何方法可能满足您的需求。