Django: normalize/modify a field in model serializer

Question:

I have a model serializer like this:

class FoooSerializers(serializers.ModelSerializer):
    class Meta:
        model = Food
        fields = [
            'id',
            'price',]

Here I have the price with trailing zeros like this: 50.000 and I want to .normalize() to remove the trailing zeros from it.
Is this possible to do that in this serializer?

Asked By: Mehdi Aria

||

Answers:

class FoooSerializers(serializers.ModelSerializer):
  class Meta:
    model = Food
    fields = ['id','price',]
def to_representation(self, instance):
    representation = super().to_representation(instance)
    representation['price'] = int(price)
    return representation
Answered By: Amir Gh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.