{"action":"created","issue":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3265","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3265/labels{/name}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3265/comments","events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3265/events","html_url":"https://github.com/tomchristie/django-rest-framework/issues/3265","id":100426602,"number":3265,"title":"Guard against calling `serializer.data` before `serializer.save()`","user":{"login":"kylefox","id":37838,"avatar_url":"https://avatars.githubusercontent.com/u/37838?v=3","gravatar_id":"","url":"https://api.github.com/users/kylefox","html_url":"https://github.com/kylefox","followers_url":"https://api.github.com/users/kylefox/followers","following_url":"https://api.github.com/users/kylefox/following{/other_user}","gists_url":"https://api.github.com/users/kylefox/gists{/gist_id}","starred_url":"https://api.github.com/users/kylefox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kylefox/subscriptions","organizations_url":"https://api.github.com/users/kylefox/orgs","repos_url":"https://api.github.com/users/kylefox/repos","events_url":"https://api.github.com/users/kylefox/events{/privacy}","received_events_url":"https://api.github.com/users/kylefox/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/labels/Cleanup","name":"Cleanup","color":"207de5"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-08-11T22:36:59Z","updated_at":"2015-08-12T13:47:50Z","closed_at":null,"body":"It appears that accessing `serializer.data` has side effects. Merely accessing the property inside `perform_create()` will cause the response data to change. I created a simple Django project with a test case that illustrates the bug: [django-rest-bug](https://github.com/kylefox/django-rest-bug).\r\n\r\nThe expected behavior is for `response.data` to contain the serialized version of the created object:\r\n\r\n```python\r\nclass AlbumViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):\r\n\r\n    # ... snip ...\r\n\r\n    def perform_create(self, serializer):\r\n        serializer.save()\r\n\r\n# >>> post_data = {'title': 'The Wall', 'artist': 'Pink Floyd'}\r\n# >>> response = self.client.post(reverse('album-list'), post_data)\r\n# >>> print response.data\r\n# {'id': 1, 'artist': u'Pink Floyd', 'title': u'The Wall'}\r\n```\r\n\r\nBut when `serializer.data` is present it will cause `response.data` to be equal to the initially posted data (ex: the `id` is missing from response):\r\n\r\n```python\r\nclass AlbumViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):\r\n\r\n    # ... snip ...\r\n\r\n    def perform_create(self, serializer):\r\n        serializer.data  # This line being present changes the value of `response.data`\r\n        serializer.save()\r\n\r\n# >>> post_data = {'title': 'The Wall', 'artist': 'Pink Floyd'}\r\n# >>> response = self.client.post(reverse('album-list'), post_data)\r\n# >>> print response.data\r\n# {'artist': u'Pink Floyd', 'title': u'The Wall'}\r\n```\r\n\r\nEven if `serializer.data` is not \"safe\", I think it's incorrect for property access to result in side effects. I *think* the culprit is somewhere in [`BaseSerializer.data`](https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py#L210) but am not certain.\r\n"},"comment":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/comments/130312334","html_url":"https://github.com/tomchristie/django-rest-framework/issues/3265#issuecomment-130312334","issue_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3265","id":130312334,"user":{"login":"tomchristie","id":647359,"avatar_url":"https://avatars.githubusercontent.com/u/647359?v=3","gravatar_id":"","url":"https://api.github.com/users/tomchristie","html_url":"https://github.com/tomchristie","followers_url":"https://api.github.com/users/tomchristie/followers","following_url":"https://api.github.com/users/tomchristie/following{/other_user}","gists_url":"https://api.github.com/users/tomchristie/gists{/gist_id}","starred_url":"https://api.github.com/users/tomchristie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomchristie/subscriptions","organizations_url":"https://api.github.com/users/tomchristie/orgs","repos_url":"https://api.github.com/users/tomchristie/repos","events_url":"https://api.github.com/users/tomchristie/events{/privacy}","received_events_url":"https://api.github.com/users/tomchristie/received_events","type":"User","site_admin":false},"created_at":"2015-08-12T13:47:50Z","updated_at":"2015-08-12T13:47:50Z","body":"Not critical no, but preferable, so let's keep this open for now."}}