django admin 中基于用户的模型实例过滤
我正在使用 django 的 admin 来让用户管理特定模型的模型实例。 每个用户应该只能管理他的模型实例。 (管理员除外,他们应该管理所有内容)。
如何过滤管理员更改列表视图中的对象?
想法:
- 我想最优雅的方法是使用 Object级权限。有人知道这个的实施吗?
- 是否可以通过使用 ModelAdmin.changelist_view 覆盖管理员的视图来完成?
- list_select_lated 与它有什么关系吗?
I'm using django's admin to let users manage model instances of a specific model.
Each user should be able to manage only his model instances. (except for administrators which should manage all).
How do I filter the objects in the admin's changelist view?
Thoughts:
- I guess the most elegant approach would be to use Object-level permissions. Anyone aware of an implementation of this?
- Is it possible to do by overriding the admin's view using ModelAdmin.changelist_view?
- Does list_select_related have anything to do with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以覆盖管理员的
queryset
方法以仅显示用户的项目:除此之外,您还应该注意
has_change_permission
和has_delete_permission
方法,例如:与
has_change_permission
相同!list_select_lated
仅在获取管理员的查询集以立即从关系中获取相关数据时使用,而不是在需要时使用!如果您的主要目标只是限制用户无法使用其他对象,则上述方法将起作用,如果它变得更加复杂并且您无法简单地从一个属性(例如用户)来区分权限,请查看django 的行级权限建议!
You can override the admin's
queryset
-method to just display an user's items:Besides that you should also take care about the
has_change_permission
andhas_delete_permission
-methods, eg like:Same for
has_change_permission
!list_select_related
is only used when getting the admin's queryset to get also related data out of relations immediately, not when it is need!If your main goal is only to restrict a user to be not able to work with other's objects the above approch will work, if it's getting more complicated and you can't tell a permissions simply from ONE attribute, like user, have a look into django's proposal's for row level permissions!