Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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
Source by gist.githubusercontent.com #
 
PREVIOUS NEXT
Tagged: #How #hook #django #rest #serializers
ADD COMMENT
Topic
Name
2+5 =