def to_representation(self, obj):
        if obj is None:
            return super(StatsInstanceSerializer, self).to_representation(obj)

        request = self.context.get('request')
        method = request.query_params.get('method', None)
        field = request.query_params.get('field', None)

        if field and field not in obj.data_dictionary().get_keys():
            raise exceptions.ParseError(detail=_("Field not in XForm."))

        stats_function = STATS_FUNCTIONS.get(method and method.lower(),
                                             get_all_stats)

        try:
            data = stats_function(obj, field)
        except __HOLE__ as e:
            raise exceptions.ParseError(detail=e.message)

        return data
dataset/ETHPy150Open kobotoolbox/kobocat/onadata/libs/serializers/stats_serializer.py/StatsInstanceSerializer.to_representation