【发布时间】:2017-07-12 06:09:20
【问题描述】:
对象级权限
class IsOwnerOrReadOnly(permissions.BasePermission):
"""
Custom permission to only allow owners of an object to edit it.
"""
def has_object_permission(self, request, view, obj):
# Read permissions are allowed to any request,
# so we'll always allow GET, HEAD or OPTIONS requests.
if request.method in permissions.SAFE_METHODS:
return True
# Write permissions are only allowed to the owner of the snippet.
return obj.owner == request.user
我的需求:用户可以编辑的所有对象的查询集
我想要一个 django-orm 查询集,其中包含给定用户可以编辑的所有对象。
我想我可以通过创建一个复杂的 django-orm 过滤器(带有 OR 和 distinct)来解决这个问题
不干燥
但这不是 DRY。这不是 DRY,因为我需要对这些东西进行两次编码。一次在has_object_permission() 中,一次在 django-orm 过滤器中。
问题
如何在不重复权限检查的情况下解决我的需求(用户可以编辑的所有对象的查询集)?
【问题讨论】:
标签: django django-rest-framework dry