You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_writable_fields in Serializer retrieves read only fields #4666
Hi,
the _writable_fields in the Serializer class decides if a field is writeable or not is this way:
if (not field.read_only) or (field.default is not empty)
This has caused an issue for us. I have a serializer with 3 nested fields that are defined as read only. So when I send the serializer to the client, these fields are not empty. When I want to update this object and send the data back to the server, these fields are again not empty. The fields are not changed, since it's read only, they just have the same data they had before.
But when I reach the validation point, because this fields are not empty, they are added to the writeable_fields list, even though they are defined as read only, like this:
offer = OfferSerializer(read_only=True)
In this case, when we tried to update the object we got this error:
The .update() method does not support writable nested fields by default.
Write an explicit .update() method for serializer...
I don't get your issue.
This only case where the field will be marked as writable is when you have a default value associated.
Sounds like you just need to pass the nested serializer value as an extra argument to the serializer.save instead.
Will consider reopening with an associated test case that highlight the issue as it's not clear to me where it actually is.
Activity
xordoquy commentedon Nov 9, 2016
I don't get your issue.
This only case where the field will be marked as writable is when you have a default value associated.
Sounds like you just need to pass the nested serializer value as an extra argument to the serializer.save instead.
Will consider reopening with an associated test case that highlight the issue as it's not clear to me where it actually is.
danielkeller commentedon Mar 19, 2018
I'm having this issue with fields like
The nested writes test complains about this field when it shouldn't.
tagis a nullable foreign key, sodefaultis necessary to get{"year": null}in the output when tag is NULL (otherwise the year key is simply omitted).carltongibson commentedon Mar 19, 2018
@danielkeller I'm looking into this issue right now (branch, PR tomorrow).
Can you show me the error and the end of your traceback? All data is good.
carltongibson commentedon Mar 19, 2018
I'm going to reopen this. It'll give me something to close. 🙂
carltongibson commentedon Mar 19, 2018
Related: #5708 (comment)
carltongibson commentedon Mar 20, 2018
Closed in #5886