[Fixed]-How to captalize letter on django rest serialize

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()

Leave a comment