I want to have the following behavior:
# GET Request
{
"id": 1,
"name": "test",
"created_date": "date",
"completed_date": "date",
"template": { "name" : "test" }, => nested serializers with only the field "name"
"status": 1,
"results": [ { __all__ }, ... ], => nested serializers with all the fields
"groups": [ { "name" }, ... ], => nested serializers with only the field "name"
}
# POST Request
{
"name": "test",
"template": {"name":"test"}, => nested serializers with only the field "name"
"groups": [ {"name":"test"} ], => nested serializers with only the field "name"
}
As you can see the post request doesn't have all the information that get displays. I know that some of the fields can be removed with the read_only_fields
variable. But the issue here is more related to the nested serializers. I have trouble validating the data... This issue is related to this issue: Dynamic nested serializers: empty validated_data
I was wondering if using dynamic serializers and with the combination of read_only_fields
was a good maintainable and readable solution:
https://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields
Or should I use the following solution: Use different serializer for request and reply
PS: I found this post too which is similar to what I am trying to do: Django Rest Framework : Nested Serializer Dynamic Model Fields
Thanks again !
Regards,