{"action":"closed","issue":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2426","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2426/labels{/name}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2426/comments","events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2426/events","html_url":"https://github.com/tomchristie/django-rest-framework/issues/2426","id":54703501,"number":2426,"title":"Respect to_field property of ForeignKey relations","user":{"login":"Harper04","id":31411,"avatar_url":"https://avatars.githubusercontent.com/u/31411?v=3","gravatar_id":"","url":"https://api.github.com/users/Harper04","html_url":"https://github.com/Harper04","followers_url":"https://api.github.com/users/Harper04/followers","following_url":"https://api.github.com/users/Harper04/following{/other_user}","gists_url":"https://api.github.com/users/Harper04/gists{/gist_id}","starred_url":"https://api.github.com/users/Harper04/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Harper04/subscriptions","organizations_url":"https://api.github.com/users/Harper04/orgs","repos_url":"https://api.github.com/users/Harper04/repos","events_url":"https://api.github.com/users/Harper04/events{/privacy}","received_events_url":"https://api.github.com/users/Harper04/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-01-18T17:35:38Z","updated_at":"2015-01-19T10:50:23Z","closed_at":"2015-01-19T10:50:23Z","body":"In my project i use ForeignKeys with the to_field property to link on a uuid Field. That UUID field is Unique but NOT the primary key for a few reasons. (Including that mysql uses the dumbest indextype possible for primary keys and i need to scale. you cannot change that)\r\n\r\nI do also have multiple peers with possible network outage that have to create database entries. So using UUIDs all over is the only feasable way. Therefore i need to serialize my ForeignKeys to the uuid field specified in to_field and deserialize from it.\r\n\r\nDespite my unusual requirements i think using to_field to unique but not primary keys is perfectly fine and should be supported.\r\n\r\n```python \r\n>>> b = Statement(device_id=\"335680b80f204e25a0e23caa052a3e83\", rule_id=\"d19b314a8cb04a558798d6d8026cc56e\", evalOperator=\"EQ\", evalDesiredState=False)\r\n>>> x=StatementSerializer(b)\r\n>>> json = JSONRenderer().render(x.data)\r\n>>> json\r\n'{\"id\":null,\"created\":null,\"modified\":null,\"uuid\":\"8846412b-5b07-485a-9f2b-d41c0902a98c\",\"evalOperator\":\"EQ\",\"evalDesiredState\":false,\"device\":\"335680b80f204e25a0e23caa052a3e83\",\"rule\":\"d19b314a8cb04a558798d6d8026cc56e\"}'\r\n```\r\nSerialization works kind of. Notice that i have to/can use key_id syntax -> device: <uuid>\r\n```python \r\n>>> mod_json = json.replace('rule','rule_id').replace('device',\"device_id\").replace('user',\"user_id\")\r\n>>> json_data = JSONParser().parse(BytesIO(json))\r\n>>> mjson_data = JSONParser().parse(BytesIO(mod_json))\r\n>>> xx=StatementSerializer(data=json_data)\r\n>>> xx.is_valid()\r\nFalse\r\n>>> xx.errors\r\nReturnDict([('device', [u'Incorrect type. Expected pk value, received unicode.']), ('user', [u'Incorrect type. Expected pk value, received unicode.']), ('rule', [u'Incorrect type. Expected pk value, received unicode.'])])\r\n\r\n\r\n>>> xx=StatementSerializer(data=mjson_data)\r\n>>> x.is_valid()\r\nFalse\r\n>>> x.errors\r\nReturnDict([('device', [u'This field is required.']), ('user', [u'This field is required.']), ('rule', [u'This field is required.'])])\r\n```\r\nNote: I also tried replacing <key> with <key>_id for deserialization but even that does not work. IMHO it should.\r\n\r\n\r\n\r\n```python\r\nclass PrimaryKeyRelatedField(RelatedField):\r\n    def use_pk_only_optimization(self):\r\n        return True\r\n```\r\nA hotfix could be disabling use_pk_only_optimization in PrimaryKeyRelatedField. It deserialized correctly that way but .data etc. still shows the primary key like:\r\nReturnDict([(u'id', None), ('created', None), ('modified', None), ('uuid', u'43c93954-e9c3-4e90-9d40-c1a87f68cb73'), ('evalOperator', 'EQ'), ('evalDesiredState', False), ('user', 1L), ('device', 2L), ('rule', 2L)])\r\n\r\n"}}