同一模型的多个版本的 django 管理页面

发布于 2024-09-25 14:32:16 字数 1107 浏览 1 评论 0原文

在我的 django 管理部分,我想根据当前登录的用户类型显示管理页面的不同版本。我可以想到几种可行的方法,但还没有弄清楚如何执行任何操作其中。

也许我可以将逻辑放入 admin.ModelAdmin 中以查看当前用户并动态更改“排除”字段。那有用吗?或者可以根据登录者运行不同的自定义模板,并让模板根据需要包含/排除字段。

我可以注册两个版本的 admin.ModelAdmin 类,每种类型的用户一个,并且可能通过权限限制访问?但权限系统似乎相当深入地相信每个模型类的一组权限,所以我不知道如何改变它。

我可以获取几个用于呈现管理页面模板的小部件,并将它们包含在我自己的页面中,该页面完成我需要强大的用户能够完成的一项特定工作。

我可以设置多个管理站点并通过 url/视图系统限制对它们的访问。但随后我不确定如何向不同的 AdminSites 注册不同的 admin.ModelAdmin 类。

任何对此的建议将不胜感激。

回答

感谢您的提示。我是这样做的......

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)

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.

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.

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.

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.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2024-10-02 14:32:16

ModelAdmin 类中为此类事情提供了相当多的钩子。

一种可能是重写 get_form 方法。这需要请求以及正在编辑的对象,因此您可以从那里获取当前用户,并根据用户返回不同的模型表单。

值得查看 ModelAdmin 的源代码 - 它位于 django.contrib.admin.options 中 - 看看重写此方法或任何其他方法是否可以满足您的需求。

There are quite a few hooks provided in the ModelAdmin class for this sort of thing.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文