Why we use super().perform_update(serialixer) when we redefining methods?

82 views Asked by At

Why we use super().perform_update(serializer)? But not serializer.save() ? What's the difference?

def perform_update(self, serializer):
        if serializer.instance.author != self.request.user:
            raise PermissionDenied('Сhange other people\'s content forbidden')
        super(PostViewSet, self).perform_update(serializer)
        #serializer.save()


1

There are 1 answers

0
JayaAnim On

When you pass a mixin to your own you pass its methods to it as well. In your code here you are overriding the update_view method but calling super().perform_update() to keep the original functionality of the method.

The perform_update method comes from the UpdateModelMixin. When serializer.save() is called it calls update(), which calls perform_update(). Perform_update() will now have the additional functionality that you just gave it in this code.

You do not want to call the save(), because django handles that itself, similar to how you almost never need to call render() when passing a view class to your own. Instead you are indirectly modifying the save method for when it is called.

Please view this documentation to see the functionality of these views and how they work. Seeing the code will give you a much better understanding: https://www.django-rest-framework.org/api-guide/generic-views/