My rest api view defines only two methods:
class RequestSerializerViewSet(mixins.CreateModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet):
i have some experience overwriting the list, but i have no experience overwriting create as it seems to be a lot more complicated.
What i want to achieve is following. My Request model has a special method which creates instance of Requests – start_request. This method creates not only the instance of Request, but also some additional foreign objects and assings them to the newly created Request.
So my goal would be to modify rest api POST (create, perform_create, or serializer – i have no idea atm. which is the correct way) method so, that it does not create the Request on its own, instead, uses start_request method of the model.
What would be the best django approach to this?
You can redefine
create method in your Serializer. Let’s assume you have
class RequestSerializer(serializers.ModelSerializer): class Meta: model = Request fields = "__all__" def create(self, validated_data): request = Request.start_request(**validated_data) # pass all parameters from serializer return request
Answered By – Nikita