[Fixed]-Creating and consuming an API using Django and the Django rest framework

2👍

Serializers should do one thing only: serializing data. They should not implement any business logic. As I understand your code example, do_calc is implementing business logic. This is not the correct place, moreover, you actually return the output of a different serializer. This code belongs into the view.

In short, I don’t understand why you are using DRF serializers at this point at all. Either you want to use the framework or you simply roll your own logic, wich is perfectly fine. Serialization is not that much work if you only have these two values (unless your example is totally shortened).

-1👍

is_valid only use when you want to update and create a model instance.
function do_calc is not proper way to serialize model use to_representation and do your work in that.

Leave a comment