Skip to content

Accessing serializer.data in a validate method shouldn't return stale data #2276

Closed
@pzrq

Description

@pzrq

Example test case I think illustrates the issue:
https://github.com/mathspace/django-rest-framework/compare/validation-race

If I call self.data inside a serializer.validate, the existing stale instance data ends up cached on serializer._data. The workaround that passed my tests was to access serializer.instance instead.

Curious as to thoughts on this, e.g.

  • Should serializer.data be accessible inside the validate methods, i.e. while the serializer is essentially still under construction?
  • Or should the _data cache be appropriately flushed so the test can pass?

I would lean towards the former only because the serializer.data by being partly constructed could be quite misleading.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions