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?
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
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?
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