[Django]-Update model fields based on POST data before save with Django Rest Framework

3👍

From official docs:

The one difference that you do need to note is that the .clean() method will not be called as part of serializer validation, as it would be if using a ModelForm. Use the serializer .validate() method to perform a final validation step on incoming data where required.

There may be some cases where you really do need to keep validation logic in the model .clean() method, and cannot instead separate it into the serializer .validate(). You can do so by explicitly instantiating a model instance in the .validate() method.

def validate(self, attrs):
    instance = ExampleModel(**attrs)
    instance.clean()
    return attrs

Leave a comment