Answers for "How to use "to_representation" hook for django rest serializers"

0

How to use "to_representation" hook for django rest serializers

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('email', 'first_name', 'last_name')

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if instance.is_superuser:
            representation['admin'] = True
        return representation
Posted by: Guest on May-02-2022

Python Answers by Framework

Browse Popular Code Answers by Language