[Solved]-Rest Framework Serializer Method

22👍

Serializers are passed a context dictionary, which contains the view instance, so you could get the profile_id by doing something like this:

view = self.context['view']
profile_id = int(view.kwargs['profile_id'])

However in this case I don’t think you need to do that as ‘obj’ will in any case be set to the profile instance.

And yes, you could put the ‘get_this_score’ method on the model class instead. You’d still need the ‘SerializerMethodField’, but it would simply call ‘return obj.get_this_score(…)’, setting any arguments from the serializer context.

Note that the serializer context will also include ‘request’, so you can also access ‘request.user’ if needed.

7👍

To answer jason’s question response to Tom’s answer you can access the request object via the same context mechanism like so. You reference the request objects from the ModelMethod definition, you don’t pass them. I was able to use this to access the current request.user object as below:

class ProductSerializer(serializers.ModelSerializer):
    score = serializers.SerializerMethodField('get_this_score')

    class Meta:
        model = Product
        fields = ('id', 'title', 'active', 'score')

    def get_this_score(self, obj):

        profile = self.context['request'].user
        score = [val for val in obj.attribute_answers.all() if val in profile.attribute_answers.all()]
        return (len(score))
👤Emeka

Leave a comment