Stack Exchange
Pass argument in method called through SerializerMethodField
I defined a serializer using the following code:
class TermSerializer(serializers.ModelSerializer):

    associatedVocabs = serializers.SerializerMethodField('get_assocVocabs')
    equivalentVocabs = serializers.SerializerMethodField('get_equivVocabs')

    def get_assocVocabs(self,obj):
        vocabs = Vocab.objects.none()
        related = RelationTerm.objects.filter(from_term = obj.id, relation=1)
        return related

    def get_equivVocabs(self,obj):
        vocabs = Vocab.objects.none()
        related = RelationTerm.objects.filter(from_term = obj.id, relation=2)
        return related

    class Meta:
        model = Term
        fields = ('id', 'associatedVocabs', 'equivalentVocabs')

As you can see in this serializer I defined two methods:get_assocVocabs and get_equivVocabs. 
They are exactly the same, what changes is the value of relation in filter, 1 is to filter associated vocabs, and 2 is to get equivalent vocabs.
By the way, this is my viewset:
class TermViewSet(viewsets.ModelViewSet):
    renderer_classes = (TermJSONRenderer, BrowsableAPIRenderer, )
    serializer_class = TermSerializer
    allowed_methods = ('GET','OPTIONS','HEAD')

    def get_queryset(self):
        queryset = Term.objects.all()
        word = self.request.query_params.get('word', None)
        if word is not None:
            queryset = queryset.filter(word__iexact=word)
        return queryset

What I need is to avoid having two equal methods inside the serializer, when I can have just one (because they do the same) if I pass an argument to filter the relation field.
How can I do this?
Thanks in advance ;)

Is there any relation between RelationTerm.relation and the Term object?

I just want to know how to pass an argument to the method 'get_assocVocabs' using SerializerMethodField. Why do you need to know that?

Rather new to django, I'm having the exact same use case. Did you ever find out?

I'd add your obj to the serializer's context and fetch it from your method.
