{"action":"created","issue":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3936","repository_url":"https://api.github.com/repos/tomchristie/django-rest-framework","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3936/labels{/name}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3936/comments","events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3936/events","html_url":"https://github.com/tomchristie/django-rest-framework/pull/3936","id":133914923,"number":3936,"title":"Fix None UUID ForeignKey serialization","user":{"login":"carltongibson","id":64686,"avatar_url":"https://avatars.githubusercontent.com/u/64686?v=3","gravatar_id":"","url":"https://api.github.com/users/carltongibson","html_url":"https://github.com/carltongibson","followers_url":"https://api.github.com/users/carltongibson/followers","following_url":"https://api.github.com/users/carltongibson/following{/other_user}","gists_url":"https://api.github.com/users/carltongibson/gists{/gist_id}","starred_url":"https://api.github.com/users/carltongibson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carltongibson/subscriptions","organizations_url":"https://api.github.com/users/carltongibson/orgs","repos_url":"https://api.github.com/users/carltongibson/repos","events_url":"https://api.github.com/users/carltongibson/events{/privacy}","received_events_url":"https://api.github.com/users/carltongibson/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/labels/Needs%20further%20review","name":"Needs further review","color":"c7def8"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-02-16T08:30:39Z","updated_at":"2016-03-13T19:45:55Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/3936","html_url":"https://github.com/tomchristie/django-rest-framework/pull/3936","diff_url":"https://github.com/tomchristie/django-rest-framework/pull/3936.diff","patch_url":"https://github.com/tomchristie/django-rest-framework/pull/3936.patch"},"body":"Replaces #3915\r\n\r\n> A nullable foreign key with a UUID primary key on the other end would result in incorrect serialisation as (the string) 'None'.\r\n> \r\n> This PR adds a minimal test case and fix."},"comment":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/comments/196035576","html_url":"https://github.com/tomchristie/django-rest-framework/pull/3936#issuecomment-196035576","issue_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/3936","id":196035576,"user":{"login":"carltongibson","id":64686,"avatar_url":"https://avatars.githubusercontent.com/u/64686?v=3","gravatar_id":"","url":"https://api.github.com/users/carltongibson","html_url":"https://github.com/carltongibson","followers_url":"https://api.github.com/users/carltongibson/followers","following_url":"https://api.github.com/users/carltongibson/following{/other_user}","gists_url":"https://api.github.com/users/carltongibson/gists{/gist_id}","starred_url":"https://api.github.com/users/carltongibson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carltongibson/subscriptions","organizations_url":"https://api.github.com/users/carltongibson/orgs","repos_url":"https://api.github.com/users/carltongibson/repos","events_url":"https://api.github.com/users/carltongibson/events{/privacy}","received_events_url":"https://api.github.com/users/carltongibson/received_events","type":"User","site_admin":false},"created_at":"2016-03-13T19:45:55Z","updated_at":"2016-03-13T19:45:55Z","body":"Where a foreign key is null [`RelatedField.get_attribute`](https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/relations.py#L142-L157) will return `None` is *all cases except* where `use_pk_only_optimization` returns `True`. Only then will the existing `attribute is None` check give the wrong result. \r\n\r\nThus in https://github.com/tomchristie/django-rest-framework/commit/2ef74cfa61d6f52a2a5c32151a052488d5d7e9e2 I resolve the `pk` value before checking for `None`. This allows the _ad hoc_ code added to `UUIDField` to be removed, and the added tests still pass. 🎉\r\n\r\nI'm happy to adjust the formatting of the `A if B else C` if you don't like that. \r\n\r\nOtherwise I think this is probably the right fix. Thoughts? "}}