How to restrict update of a record to the record owner in Django REST?

85 views
Skip to first unread message

Daniel Grace

unread,
Jul 4, 2015, 11:38:48 AM7/4/15
to django...@googlegroups.com
I want to restrict update of a record to the record owner in an UpdateAPIView with Django REST, but I don't know how to code the method.

For example, something like this:

from rest_framework import generics
from testapp.serializers import UserProfileSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import permissions
from oauth2_provider.ext.rest_framework import TokenHasReadWriteScope

class UserProfileView(generics.UpdateAPIView):
    permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
    serializer_class = UserProfileSerializer
    queryset = UserProfile.objects.all()
    # patch method?
    # if UserProfile user != self.request.user:
    #     raise exceptions.PermissionDenied
    # else:
    #     continue as normal

Where "user" is a field on the UserProfile model.

Daniel Grace

unread,
Jul 4, 2015, 6:29:19 PM7/4/15
to django...@googlegroups.com
I found a solution based on the example in REST API guide:
Reply all
Reply to author
Forward
0 new messages