Как мы можем изменить разрешения таким образом, чтобы параметры запроса учитывались в Django
Для контекста, я пытался изменить PostPermissions для метода has_object_permission таким образом, чтобы пользователям было разрешено делать PATCH-запросы для изменения лайков постов, сделанных другими пользователями. Однако мой код не работает, и я не уверен, почему. Любая помощь будет высоко оценена.
Мой код для PostPermissions выглядит следующим образом:
class PostPermissions(permissions.BasePermission):
#Allow authenticated users to access endpoint
def has_permission(self, request, view):
return True if request.user.is_authenticated or request.user.is_superuser else False
def has_object_permission(self, request, view, obj):
#allow anyone to view posts through safe HTTP request (GET, OPTIONS, HEAD)
if request.method in permissions.SAFE_METHODS:
return True
data = QueryDict(request.body)
#allow users to edit the likes and dislikes of other posts
if request.method == 'PATCH' and ('likes' in data or 'dislikes' in data):
return True
#allow users to edit only their own posts through POST requests by checking that the author of the post is the same as the authenticated user
#superusers can edit and delete everyone's posts
return request.user == obj.name or request.user.is_superuser
Ошибка, которую я получил в Postman, выглядит следующим образом: