3
votes

I have an API endpoint that has a problem when i try to do a get or partial update. I am not sure what I am missing, I have included the code with the attempt and the error it gives.

The error:

"detail": "Not found."

As far as i understand the url should include the ID of the instance i would like to partially update, so the url to perform the PATCH on is:

http://localhost:8000/manuscripts-api/manuscriptlibrary/9/

My view:

class ManuscriptLibraryViewSet(viewsets.ModelViewSet):
    """Handles creating, reading and updating manuscript library."""

    authentication_classes = (TokenAuthentication,)
    serializer_class = serializers.ManuscriptLibrarySerializer
    queryset = models.ManuscriptLibrary.objects.all()
    permission_classes = (permissions.PostOwnManuscriptLibrary,)

    def put(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)

My model:

class ManuscriptLibrary(models.Model):
    """Represents a single manuscript's library"""

    manuscript = models.OneToOneField(ManuscriptItem, on_delete=models.CASCADE)
    bookmarks = models.CharField(max_length=99999999)
    history = models.CharField(max_length=99999999)

    def __str__(self):
        """Django uses when it needs to convert the object to a string"""
        return str(self.manuscript)

My serializer:

class ManuscriptLibrarySerializer(serializers.ModelSerializer):
    """A serializer for a manuscript's library."""

    class Meta:
        model = models.ManuscriptLibrary
        fields = ('id', 'manuscript', 'bookmarks', 'history')

My urls:

router = DefaultRouter()
router.register('manuscripts', views.ManuscriptViewSet, base_name="manuscripts")  # auto basename for models
router.register('manuscriptlibrary', views.ManuscriptLibraryViewSet, base_name="manuscript_library")
router.register('manuscriptsettings', views.ManuscriptSettingsViewSet)

urlpatterns = [
    url(r'', include(router.urls))
]

My permission:

class PostOwnManuscriptLibrary(permissions.BasePermission):
    """Allow author to update a manuscript's library they own."""

    def has_object_permission(self, request, view, obj):
        if request.method in permissions.SAFE_METHODS:
            return True
        return obj.manuscript.author == request.user

    def has_permission(self, request, view):
        if request.method == 'POST':
            manuscript = request.data.get('manuscript')
            instance = ManuscriptItem.objects.get(pk=manuscript)
            return instance.author == request.user
        return True

Additional Information:

Url:

http://localhost:8000/manuscripts-api/manuscriptlibrary/

Returns:

[ { "id": 28, "manuscript": 9, "bookmarks": "test success", "history": "test success" } ]

1
it can be because user is not owner by permissions PostOwnManuscriptLibraryBrown Bear
do i need to extend my permission for a method update or patch?NewScientists
when you just do get on the url with pk is all right, and you see the object serializer?Brown Bear
No I also have a problem with GET! i updated my question to mention this. GET: localhost:8000/manuscripts-api/manuscriptlibrary/9. Result: "detail": "Not found."NewScientists
add output for get http://localhost:8000/manuscripts-api/manuscriptlibrary/Brown Bear

1 Answers

2
votes

for patch and get detail you need to use values from id, in your case this is 28, try go to path, and patch

http://localhost:8000/manuscripts-api/manuscriptlibrary/28/