{"action":"created","issue":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2754","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2754/labels{/name}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2754/comments","events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2754/events","html_url":"https://github.com/tomchristie/django-rest-framework/pull/2754","id":63743260,"number":2754,"title":"Enhancement dont require pk strictly related #2745","user":{"login":"bleib1dj","id":524842,"avatar_url":"https://avatars.githubusercontent.com/u/524842?v=3","gravatar_id":"","url":"https://api.github.com/users/bleib1dj","html_url":"https://github.com/bleib1dj","followers_url":"https://api.github.com/users/bleib1dj/followers","following_url":"https://api.github.com/users/bleib1dj/following{/other_user}","gists_url":"https://api.github.com/users/bleib1dj/gists{/gist_id}","starred_url":"https://api.github.com/users/bleib1dj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bleib1dj/subscriptions","organizations_url":"https://api.github.com/users/bleib1dj/orgs","repos_url":"https://api.github.com/users/bleib1dj/repos","events_url":"https://api.github.com/users/bleib1dj/events{/privacy}","received_events_url":"https://api.github.com/users/bleib1dj/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/milestones/21","html_url":"https://github.com/tomchristie/django-rest-framework/milestones/3.1.2%20Release","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/milestones/21/labels","id":1023197,"number":21,"title":"3.1.2 Release","description":"","creator":{"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},"open_issues":6,"closed_issues":2,"state":"open","created_at":"2015-03-16T12:10:40Z","updated_at":"2015-03-23T15:30:56Z","due_on":null,"closed_at":null},"comments":4,"created_at":"2015-03-23T14:54:29Z","updated_at":"2015-03-23T16:00:47Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/pulls/2754","html_url":"https://github.com/tomchristie/django-rest-framework/pull/2754","diff_url":"https://github.com/tomchristie/django-rest-framework/pull/2754.diff","patch_url":"https://github.com/tomchristie/django-rest-framework/pull/2754.patch"},"body":"## Rationale ##\r\nI implemented the enhancement with the `id_field` to provide users using non-django ORM backends the ability to still utilize the check originally provided by `if obj.pk is None`, prior to providing a related reference. I believe this gives more flexibility but also potentially provides unnecessary functionality as alternative backends may not generate an identification value solely upon storage and could potentially have their identifier provided prior to save. If the backend does that this check would only serve to verify that the user properly assigned an identifier but would not provide the intended affect of verifying the object has been created. That being said, there is the potential for this to also be done with the django ORM as the following example illustrates:\r\n\r\n```\r\nfrom django.contrib.auth.models import User\r\nfrom uuid import uuid1\r\n\r\nnew_uuid = str(uuid1())\r\nmy_user = User(pk=new_uuid, email=\"this@email.com\", username=\"test_user\")\r\nprint my_user.pk\r\nquery_user = User.objects.get(pk=new_uuid)\r\n```\r\n```\r\nOut: '5412f4cc-d166-11e4-b274-080027a2dd8c'\r\nOut: DoesNotExist: User matching query does not exist\r\n```\r\n\r\nThis goes against using `User.objects.create(...)` but could be done. In either case providing the `id_field` gives the user the ability to choose, where `hasattr()` limits the check to only working when the `pk` attribute is present.\r\n\r\nIf this way of handling the enhancement is agreed upon I can add additional docs detailing the optional field and when to utilize it. Otherwise we can close out this PR and I can open one with my `hassattr()` modifications instead.\r\n\r\n\r\n## Additional references ##\r\nPer our discussion in #2745 I found a couple additional locations of `pk` attribute references. \r\n\r\n### PrimaryKeyRelatedField ###\r\nThere's a reference in the `to_representation` method but I opted not to change it as I believe the `SlugField` provides a way of achieving this functionality for a non-django ORM based backend.\r\n\r\n### ManyRelatedField ###\r\nThere was a reference in the `get_attribute` method that I swapped out with `self.id_field` which also required the definition of `self.id_field` within it's own `__init__` method.\r\n\r\nI chose to define it there rather than up in the `Field` class as the `ManyRelatedField` is \"Automagically\" created in the `RelatedField` and passed its `kwargs`. So instead of giving users the ability to set an `id_field` on every field, which would be unnecessary, this should reduce it to them only needing to set it on the `HyperlinkedRelatedField` and `HyperlinkedIdentityField` if they choose to, while still supporting `many=True`."},"comment":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/comments/85065732","html_url":"https://github.com/tomchristie/django-rest-framework/pull/2754#issuecomment-85065732","issue_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2754","id":85065732,"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-03-23T16:00:47Z","updated_at":"2015-03-23T16:00:47Z","body":"Something odd going on at the moment with the travis results never returning.\r\nLooks to be related to this awkward pep8 versioning clash.\r\nUnrelated to this pull request, but I'll look into it and get this merged after it's resolved.\r\nThanks for the input! :smile: :+1: "}}