From 5e7497bfe04fb91c1cdb70f7649b22325c9a78de Mon Sep 17 00:00:00 2001 From: sol HYUN Date: Mon, 5 Dec 2016 18:02:09 +0900 Subject: [PATCH] modify _writable_fields in serializers.py Sometimes 'self' instance is not a field (used in rest_framework) If the 'serializer' that contains 'field' is 'self', only the subelements of the serializer, the 'fields', are returned. in the case of a serializer that contains 'field' as a 'child' element, Only instances of `.source` ending with` _set '(following relationships "backward") are found using regular expressions. Then use the child elements again to find the field using '_writable_fields'. Do not modify '_readable_fields' because it does not address the issue of what is currently being viewed. --- rest_framework/serializers.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 02c24b70e..8e7206fd8 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -362,10 +362,15 @@ class Serializer(BaseSerializer): @cached_property def _writable_fields(self): - return [ - field for field in self.fields.values() - if (not field.read_only) or (field.default is not empty) - ] + ret = [] + for field in self.fields.values(): + if re.compile("\_set$").findall(field.source): + for field in field.child._writable_fields: + ret.append(field) + continue + if (not field.read_only) or (field.default is not empty): + ret.append(field) + return ret @cached_property def _readable_fields(self):