{"action":"created","issue":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2594","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2594/labels{/name}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2594/comments","events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2594/events","html_url":"https://github.com/tomchristie/django-rest-framework/issues/2594","id":58734443,"number":2594,"title":"Passing context through default fields","user":{"login":"maryokhin","id":4390231,"avatar_url":"https://avatars.githubusercontent.com/u/4390231?v=3","gravatar_id":"","url":"https://api.github.com/users/maryokhin","html_url":"https://github.com/maryokhin","followers_url":"https://api.github.com/users/maryokhin/followers","following_url":"https://api.github.com/users/maryokhin/following{/other_user}","gists_url":"https://api.github.com/users/maryokhin/gists{/gist_id}","starred_url":"https://api.github.com/users/maryokhin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maryokhin/subscriptions","organizations_url":"https://api.github.com/users/maryokhin/orgs","repos_url":"https://api.github.com/users/maryokhin/repos","events_url":"https://api.github.com/users/maryokhin/events{/privacy}","received_events_url":"https://api.github.com/users/maryokhin/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-02-24T12:57:09Z","updated_at":"2015-02-25T09:58:11Z","closed_at":"2015-02-25T09:20:43Z","body":"I'm not even sure if I'm doing something legal:\r\n```python3\r\nowner = serializers.HiddenField(source='user',\r\n\t\tdefault=serializers.CreateOnlyDefault(\r\n\t\t\tserializers.CurrentUserDefault()\r\n\t\t)\r\n\t)\r\n```\r\nWhich is supposed to be a write-only hidden field that gets populated only during writes, and skipped during updates (I don't allow changing FKs once an object is created).\r\n\r\nResult is:\r\n```python3\r\nRequest Method: POST\r\nRequest URL: http://127.0.0.1:8000/posts/1/ratings/\r\n\r\nTraceback:\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/django/core/handlers/base.py\" in get_response\r\n  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/django/views/decorators/csrf.py\" in wrapped_view\r\n  57.         return view_func(*args, **kwargs)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/viewsets.py\" in view\r\n  85.             return self.dispatch(request, *args, **kwargs)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/views.py\" in dispatch\r\n  407.             response = self.handle_exception(exc)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/views.py\" in dispatch\r\n  404.             response = handler(request, *args, **kwargs)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/mixins.py\" in create\r\n  20.         serializer.is_valid(raise_exception=True)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/serializers.py\" in is_valid\r\n  186.                 self._validated_data = self.run_validation(self.initial_data)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/serializers.py\" in run_validation\r\n  364.         value = self.to_internal_value(data)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/serializers.py\" in to_internal_value\r\n  397.                 validated_value = field.run_validation(primitive_value)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/fields.py\" in run_validation\r\n  364.         (is_empty_value, data) = self.validate_empty_values(data)\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/fields.py\" in validate_empty_values\r\n  345.             return (True, self.get_default())\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/fields.py\" in get_default\r\n  323.             return self.default()\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/fields.py\" in __call__\r\n  115.             return self.default()\r\nFile \"/Users/maryokhin/.virtualenvs/backend/lib/python3.4/site-packages/rest_framework/fields.py\" in __call__\r\n  129.         return self.user\r\n\r\nException Type: AttributeError at /posts/1/ratings/\r\nException Value: 'CurrentUserDefault' object has no attribute 'user'\r\n```"},"comment":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/comments/75933911","html_url":"https://github.com/tomchristie/django-rest-framework/issues/2594#issuecomment-75933911","issue_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2594","id":75933911,"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-02-25T09:58:11Z","updated_at":"2015-02-25T09:58:11Z","body":"In that case either put the `default` argument on the user field, or else stop using `HiddenField` and just put the \"set owner on create\" logic in the create method."}}