{"action":"closed","issue":{"url":"https://api.github.com/repos/django-tastypie/django-tastypie/issues/260","labels_url":"https://api.github.com/repos/django-tastypie/django-tastypie/issues/260/labels{/name}","comments_url":"https://api.github.com/repos/django-tastypie/django-tastypie/issues/260/comments","events_url":"https://api.github.com/repos/django-tastypie/django-tastypie/issues/260/events","html_url":"https://github.com/django-tastypie/django-tastypie/issues/260","id":1760428,"number":260,"title":"In ApiField hydrate/dehydrate, using getattr() on a OneToOneField can raise a DoesNotExistException","user":{"login":"jstlaurent","id":1054140,"avatar_url":"https://avatars.githubusercontent.com/u/1054140?v=3","gravatar_id":"","url":"https://api.github.com/users/jstlaurent","html_url":"https://github.com/jstlaurent","followers_url":"https://api.github.com/users/jstlaurent/followers","following_url":"https://api.github.com/users/jstlaurent/following{/other_user}","gists_url":"https://api.github.com/users/jstlaurent/gists{/gist_id}","starred_url":"https://api.github.com/users/jstlaurent/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jstlaurent/subscriptions","organizations_url":"https://api.github.com/users/jstlaurent/orgs","repos_url":"https://api.github.com/users/jstlaurent/repos","events_url":"https://api.github.com/users/jstlaurent/events{/privacy}","received_events_url":"https://api.github.com/users/jstlaurent/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2011-09-28T04:10:10Z","updated_at":"2016-01-14T00:36:36Z","closed_at":"2016-01-14T00:36:35Z","body":"I ran into this issue today. The way the *hydrate* and *dehydrate* methods in ApiField use *getattr()* can result in a DoesNotExist exception being raised rather than None being returned when the attribute being \"get\" is a non-existent OneToOneField. \r\n\r\nAs far as I can tell, since OneToOneField eschews the related manager and tries to return the related object directly, it executes a query that finds no result and thus raises the exception.\r\n\r\nSpecifically here:\r\n\r\n```python\r\nif self.blank:\r\n     return None\r\nelif self.attribute and getattr(bundle.obj, self.attribute, None):\r\n     return getattr(bundle.obj, self.attribute)\r\nelif self.instance_name and hasattr(bundle.obj, self.instance_name):\r\n     return getattr(bundle.obj, self.instance_name)\r\n```\r\nand here:\r\n\r\n```python\r\nfor attr in attrs:\r\n    previous_object = current_object\r\n    current_object = getattr(current_object, attr, None)\r\n```\r\n\r\nMy solution was to replace the direct use of *getattr()* with checking first with *hasattr()*. *hasattr()* will return False without raising an exception if there is no related object. I've made a fix in my fork. I'll send a pull request, if it's alright."}}