Search code examples
djangodjango-rest-frameworkdjango-viewsdjango-serializer

Which update method to override? in the mixin or the serializer?


While I am working on a project I find myself need to add some logic in the update of some entity in my model, but I don't know whether to add this logic in the update method in the UpdateModelMixin or the update method in the ModelSerializer. What is the difference between them?

I searched everywhere and didn't find any specific or general answer to that question


Solution

  • You can approach it in two ways: either override the update method in the serializer or the perform_update method at the Viewset level. The choice depends on your specific use case.

    If you need to include additional data during the update that is not part of the serializer fields, then you should override the update method within the serializer. On the other hand, if you need to perform some third-party API call after the update operation, it's better to override the perform_update method at the Viewset level.