I have a situation where my client is attempting to write a representation that includes a list of fk\'s
{
languages: [1]
last_name: \"Beecher\"
settings: 1
stat
I had the same problem and it looks like quite a few others have it too. Carlton Gibson answer actually lead me to my hacky solution. I ended up using a ModelSerializer with the depth set and created the following mixin to use in the views.
class ReadNestedWriteFlatMixin(object):
"""
Mixin that sets the depth of the serializer to 0 (flat) for writing operations.
For all other operations it keeps the depth specified in the serializer_class
"""
def get_serializer_class(self, *args, **kwargs):
serializer_class = super(ReadNestedWriteFlatMixin, self).get_serializer_class(*args, **kwargs)
if self.request.method in ['PATCH', 'POST', 'PUT']:
serializer_class.Meta.depth = 0
return serializer_class