如何防止 Django 管理员用户更改其他管理员用户个人资料数据?
我有按教师类别扩展/子类化的管理员用户。
如何防止教师查看和更改其他教师的个人资料数据并且教师只能更改自己的记录/行?提前致谢!
I have Admin User extended/subclassed by Teacher class.
How to prevent Teachers from seeing and changing other Teachers' profile data and Teachers are able to change their own records/rows only? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否完全理解您想要做什么,但如果您的想法是让内置用户管理页面对教师用户的工作方式略有不同,那么我相信您只需扩展
UserAdmin
,并重写queryset
方法。这将禁止教师编辑或删除其他记录,因为如果您查看
ModelAdmin
代码,change_view
和delete_view
方法将使用从queryset
方法返回的 queryset 来获取要更改或删除的对象。还需要进行一项调整,因为用于更改 UserAdmin 中密码的视图不使用与其他视图相同的系统来更改对象。只需在您的新班级中覆盖它即可:
之后,您只需阻止教师添加新用户或删除他们自己的记录即可。使用默认的 django 的权限框架,或者覆盖 < code>has_add_permission 和
has_delete_permission
方法。如果您需要更多信息,请查看
ModelAdmin
源代码(在contrib/admin/options.py
中)。I'm not sure I understand exactly what you're trying to do, but if what's in your mind is having the built-in user administration pages working slightly differently for Teacher users, then I believe you just have to extend
UserAdmin
, and override thequeryset
method.That will take care of disallowing Teachers to edit or delete other records, because if you look in the
ModelAdmin
code,change_view
anddelete_view
method use the queryset returned fromqueryset
method to get the object to change or delete.One more tweak is necessary, because the view used to change the password in
UserAdmin
doesn't use the same system as the others views to get the object to change. Just override it in your new class :After that, you just have to prevent Teachers to add new users, or delete their own record. Do that either using the default django's permission framework, or by overriding
has_add_permission
andhas_delete_permission
methods.Have a look in the
ModelAdmin
source code if you want more info (incontrib/admin/options.py
).可能没有构建方法可以做到这一点。
请参阅权限文档:
但是,对象级别权限显然,我们即将到来。
There probably isn't a build in way to do this.
See the permission docs:
However, object level permissions are coming, apparently.
目前没有简单的方法可以做到这一点,但是 对象级权限 即将在 Django 1.2 中推出 - 尽管您必须做一些工作才能使其在管理中运行。
幸运的是,有一篇 Django Advent 文章 解释了您需要做什么。
There is no easy way to do this currently, but object level permissions are coming soon in Django 1.2 - although you have to do some work to get it working in the admin.
Luckily there is a Django Advent article which explains what you need to do.