Issue
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?
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.