In django rest framework, it is possible to dynamically remove fields at runtime with a custom
__init__ method on the serializer. I am using this technique to remove certain fields from the serializer depending on the relation of the requesting user and requested resource, as follows:
def __init__(self, *args, **kwargs):
# get request from context, initialize super class etc.
if request.user != self.instance.user:
This approach works well when retrieving a single object, but when I am listing objects, the serializer is initialized with a list of items, hence self.instance does not point to a specific instance on init method, hence I can not make the comparison I want.
Any suggestions on how I can achieve what I want when listing objects?