python - Django Rest Framework dynamic field list on Serializer for list of items -


in django rest framework, possible dynamically remove fields @ runtime custom __init__ method on serializer. using technique remove fields serializer depending on relation of requesting user , requested resource, follows:

def __init__(self, *args, **kwargs):     # request context, initialize super class etc.      if request.user != self.instance.user:         self.fields.pop('private field') 

this approach works when retrieving single object, when listing objects, serializer initialized list of items, hence self.instance not point specific instance on init method, hence can not make comparison want.

any suggestions on how can achieve want when listing objects?

you should call serializer keyword many=true each element processed independently, , pass through custom init. like:

serialization = myserializer(data=request.data, many=true) 

if don't if handle element or list of elements should either convert elemente list, receive lists (1 many) or detect case , define many accordingly.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -