Skip to content

_writable_fields in Serializer retrieves read only fields #4666

Closed

Description

@vasofi

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

Activity

  1. xordoquy commented on Nov 9, 2016

    @xordoquy
    Contributor

    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.

  2. danielkeller commented on Mar 19, 2018

    @danielkeller

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

  3. carltongibson commented on Mar 19, 2018

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

  4. carltongibson commented on Mar 19, 2018

    @carltongibson
    Collaborator

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

  5. carltongibson commented on Mar 19, 2018

    @carltongibson
    Collaborator
  6. added this to the milestone on Mar 19, 2018
  7. carltongibson commented on Mar 20, 2018

    @carltongibson
    Collaborator

    Closed in #5886

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions