1π
The best way is use your serializer:
Something like this:
class ServiceSerializer(serializers.ModelSerializer):
category = ServiceCategorySerializer()
service = serializers.SerializerMethodField()
def get_service(self, obj):
return obj.service.upper() if obj.service else None
This is the first way π
The second is to change tour to_representation, like this:
class ServiceSerializer(serializers.ModelSerializer):
category = ServiceCategorySerializer()
class Meta:
model = Service
fields = ('service', 'category')
def to_representation(self, instance):
data = super(ServiceSerializer, self).to_representation(instance=instance)
data['service'] = data['service'].lower() if data['service'] else data['services']
return data
The problem is that π before each save β you should reverse the process β especially in the second case. But in the first scenario β you will also get from the front the changed value, so basically after some finite time you will have all DB rows rewritten π
As someone suggested β why you do not want to do this on Front side?
π€opalczynski
0π
## First Letter Capital.
def get_name(self, obj):
return obj.name.capitalize()
Source:stackexchange.com