Django admin - 如何隐藏用户编辑中的某些字段?

发布于 2024-11-18 08:08:12 字数 54 浏览 2 评论 0原文

如何隐藏管理员用户编辑中的字段?主要是我想隐藏某些例外中的权限和组选择,但排除变量不起作用:/

How can I hide fields in admin User edit? Mainly I want to hide permissions and groups selecting in some exceptions, but exclude variable doesn't work :/

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

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

发布评论

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

评论(2

燃情 2024-11-25 08:08:12

我可能迟到了回答这个问题,但无论如何,就这样吧。 John 的概念是正确的,但我只是想这样做,因为我知道 django admin 非常灵活。

在用户模型表单中隐藏字段的任何方式都是:

1. ModelAdmin 类的 exclude 属性可用于隐藏字段。

2:模型中应允许存在空白。

3: 模型字段上的 default 属性是一个优势,否则您可能会遇到意外错误。

我遇到的问题是我经常遇到验证错误。我查看了回溯,发现
该错误是由于 UserAdminfieldsets 分组导致的,默认的 permission 字段集已在您的 user_permission 中覆盖了此设置子类模型管理员。

使用 get_form 中的 exclude 属性,您可以在其中访问 request 变量,并且可以根据用户的权限或组动态设置它。

代码:

admin.py:

class MyUserAdmin(UserAdmin): 

     list_display = ("username","first_name", "last_name", "email","is_active","is_staff","last_login","date_joined")

     ## Static overriding 
     fieldsets = (
         (None, {'fields': ('username', 'password')}),
         (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
         (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                    'groups')}),
     (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
     )


     def get_form(self, request, obj=None, **kwargs):
         self.exclude = ("user_permissions")
         ## Dynamically overriding
         self.fieldsets[2][1]["fields"] = ('is_active', 'is_staff','is_superuser','groups')
         form = super(MyUserAdmin,self).get_form(request, obj, **kwargs)
         return form

I may be late to answer this question but any ways, here goes. John is right in concept but I just wanted to do this because I know django admin is truly flexible.

Any way's the way you hide fields in User model form is:

1. exclude attribute of the ModelAdmin class can be used to hide the fields.

2: The should allow blank in model.

3: default attribute on model field is an advantage or you might get unexpected errors.

The problems I had was that I used to get a validation error. I looked at the trace back and found out that
the error was because of UserAdmin's fieldsets grouping, the default permission field set has user_permission override this in your sub-calassed model admin.

Use the exclude attribute in get_form where you can access request variable and you can set it dynamical depending on the user's permission or group.

Code:

admin.py:

class MyUserAdmin(UserAdmin): 

     list_display = ("username","first_name", "last_name", "email","is_active","is_staff","last_login","date_joined")

     ## Static overriding 
     fieldsets = (
         (None, {'fields': ('username', 'password')}),
         (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
         (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                    'groups')}),
     (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
     )


     def get_form(self, request, obj=None, **kwargs):
         self.exclude = ("user_permissions")
         ## Dynamically overriding
         self.fieldsets[2][1]["fields"] = ('is_active', 'is_staff','is_superuser','groups')
         form = super(MyUserAdmin,self).get_form(request, obj, **kwargs)
         return form
暗地喜欢 2024-11-25 08:08:12

django admin 并不是为非常细粒度的控制而设计的,因此它们没有旨在允许这种类型的控制的自动变量。

如果您需要这种类型的控制,您就必须自己进行控制。您需要覆盖默认的管理模板。您可能希望使用权限系统来跟踪允许用户执行的操作。

请记住您正在进行的定制级别。在某些时候,远远超出管理应用程序的预期目的和限制将比简单地滚动自己的更细粒度的 CRUD 系统需要更多的工作。

The django admin is not designed for very fine grained control so their are no automated variables designed to allow this type of control.

If you need this type of control you're going to have to go it your own. You'll need to override the default admin templates. You'll probably want to use the permissions system to track what users are allowed to do.

Keep in mind the level of customization you're making. At some point working to far outside the intended purpose and limitations of the admin app will be more work than simply rolling your own more fine grained CRUD system.

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