Django admin - 如何隐藏用户编辑中的某些字段?
如何隐藏管理员用户编辑中的字段?主要是我想隐藏某些例外中的权限和组选择,但排除变量不起作用:/
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能迟到了回答这个问题,但无论如何,就这样吧。 John 的概念是正确的,但我只是想这样做,因为我知道 django admin 非常灵活。
在用户模型表单中隐藏字段的任何方式都是:
1.
ModelAdmin
类的exclude
属性可用于隐藏字段。2:模型中应允许存在空白。
3: 模型字段上的
default
属性是一个优势,否则您可能会遇到意外错误。我遇到的问题是我经常遇到验证错误。我查看了回溯,发现
该错误是由于
UserAdmin
的fieldsets
分组导致的,默认的permission
字段集已在您的user_permission
中覆盖了此设置子类模型管理员。使用
get_form
中的exclude
属性,您可以在其中访问request
变量,并且可以根据用户的权限或组动态设置它。代码:
admin.py:
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 theModelAdmin
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
'sfieldsets
grouping, the defaultpermission
field set hasuser_permission
override this in your sub-calassed model admin.Use the
exclude
attribute inget_form
where you can accessrequest
variable and you can set it dynamical depending on the user's permission or group.Code:
admin.py:
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.