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.
This commit is contained in:
sol HYUN 2016-12-05 18:02:09 +09:00
parent 238783f2ed
commit 5e7497bfe0

View File

@ -362,10 +362,15 @@ class Serializer(BaseSerializer):
@cached_property @cached_property
def _writable_fields(self): def _writable_fields(self):
return [ ret = []
field for field in self.fields.values() for field in self.fields.values():
if (not field.read_only) or (field.default is not empty) 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 @cached_property
def _readable_fields(self): def _readable_fields(self):