[Django]-Django Rest Framework add new field in serializer data using existing boolean field

3👍

you can use SerializerMethodField:

class CurrentUserSerializer(serializers.ModelSerializer):
    role = serializers.SerializerMethodField('is_super')

    def is_super(self, user):
        return "ADMIN" if user.is_superuser else "WHATEVER"

    class Meta:
        model = User
        fields = ('role', 'username', 'email', 'id','first_name','last_name',"is_staff","is_superuser")

Leave a comment