[Question] dynamic included_serializers #1007
Replies: 1 comment 3 replies
-
I think what you are trying to do is not REST conform as you will end up with two instances of the same resource with different representation depending on user (see #901 (comment) for a similar discussion). To be REST conform you would need to have two different APIs one private and one public returning different resources. The model can be the same simply the resource name of the serializer needs to be set differently. (by setting If you really wanna move forward with your current idea one resource different representation you could overwrite Hope this is helpful. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I am reading the current documentation but I didn't find the way to included a serializer dynamically with the included_serializers attribute.
I think is a common feature to use X or B serializer by N attribute of the request method, request user,... For example, the user type. I add this logic to the ViewSet overriding the get_serializer_class method but now I need to add an included with this behavior.
Following, you can see a simple example of this scenario:
I would like to add in the current AuthorSerializer an included_serializer -> last_comment with the CommentPrivateSerializer or the CommentPublicSerializer depending of the request user type. Something like:
Is there any way to do this? If not, have you any idea to do it? It's possible you have in mind something so I don't mind to create a PR to add this feature.
IMHO, we could move the included serializer to the ResourceRelatedField adding a boolean attribute "includable" and use the ViewSet to get the serializer?
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions