[Answered ]-Django Rest Framework, two different approach to POST request

1👍

Django follows the rapid development and DRY principles as described in the home page.

With that in mind it means Django respects minimal codebase taking that into consideration the second approach is the way to go. Morever as @Willem Van Onsem suggested CreatAPIView will be more aligned to this as that will futher simplify the serializer logic.

Edit:

You can try this (not tested):

from rest_framework.generics import CreateAPIView

class ExampleView2(CreateAPIView):
    serializer_class = CourseSerializer

    def get_serializer(self, *args, **kwargs):
       serializer = self.get_serializer_class()
       data = self.request.data.copy()
       data['owner'] = self.request.user.pk
       kwargs['data'] = data
       return serializer_class(*args, **kwargs)

You can also try changing _mutable attribute of your request.data check this post.

Note: request.data is not always mutable it depends on the request.

Leave a comment