If i have a nested serializer:
class ChildSerializer(ModelSerializer):
class Meta:
fields = (\'c_name\', )
model = Child
class ParentSe
Ok, I have found an ultimate solution that will do exactly what was asked - pass context down to nested serializers.
To achieve that one need to override to_representation(self, instance) of the nested serializer, so it looks like:
def to_representation(self, instance):
# here we update current serializer's context (access it as self._context)
# to access parent's context we use parent.context
# if there is no parent than it's the first serializer in the chain and it doesn't need any context except for itself's
# for example (after all the checks)
self._context["request"] = self.parent.context["request"]
# and that is it! The modified context will be used for serialization as if it was passed as usually
return super().to_representation(instance)