Getting value from View to Serializer in Django

I am not sure what I am doing wrong, I tried to follow a few solution.

class MyViewSet(viewsets.ModelViewSet):
    filterset_class = IngredientFilter

def get_serializer_context(self):
    context = super().get_serializer_context()
    context['test'] = "something"
    return context

In my Serializer,

class MySerializer(BaseSerializer):
    isHighlight = serializers.SerializerMethodField()

def get_isHighlight(self, obj):
    return self.context['test']

I am getting this error,

Django Version: 3.2.7
Exception Type: KeyError
Exception Value: 'test'

Any suggestions?

Back to Top