Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_writable_fields in Serializer retrieves read only fields #4666

Closed
vasofi opened this issue Nov 9, 2016 · 6 comments
Closed

_writable_fields in Serializer retrieves read only fields #4666

vasofi opened this issue Nov 9, 2016 · 6 comments
Milestone

Comments

@vasofi
Copy link

vasofi commented Nov 9, 2016

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...

@xordoquy
Copy link
Collaborator

xordoquy commented 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.

@xordoquy xordoquy closed this as completed Nov 9, 2016
@danielkeller
Copy link

I'm having this issue with fields like

year = serializers.IntegerField(source='tag.year', read_only=True, default=None)

The nested writes test complains about this field when it shouldn't.

tag is a nullable foreign key, sodefault is necessary to get {"year": null} in the output when tag is NULL (otherwise the year key is simply omitted).

@carltongibson
Copy link
Collaborator

@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 carltongibson reopened this Mar 19, 2018
@carltongibson
Copy link
Collaborator

I'm going to reopen this. It'll give me something to close. 🙂

@carltongibson
Copy link
Collaborator

Related: #5708 (comment)

@carltongibson carltongibson added this to the 3.8 Release milestone Mar 19, 2018
@carltongibson
Copy link
Collaborator

Closed in #5886

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants