{"action":"opened","issue":{"url":"https://api.github.com/repos/django-json-api/django-rest-framework-json-api/issues/148","labels_url":"https://api.github.com/repos/django-json-api/django-rest-framework-json-api/issues/148/labels{/name}","comments_url":"https://api.github.com/repos/django-json-api/django-rest-framework-json-api/issues/148/comments","events_url":"https://api.github.com/repos/django-json-api/django-rest-framework-json-api/issues/148/events","html_url":"https://github.com/django-json-api/django-rest-framework-json-api/issues/148","id":113957700,"number":148,"title":"No straightforward way for bound methods to return relations.","user":{"login":"rollokb","id":8541009,"avatar_url":"https://avatars.githubusercontent.com/u/8541009?v=3","gravatar_id":"","url":"https://api.github.com/users/rollokb","html_url":"https://github.com/rollokb","followers_url":"https://api.github.com/users/rollokb/followers","following_url":"https://api.github.com/users/rollokb/following{/other_user}","gists_url":"https://api.github.com/users/rollokb/gists{/gist_id}","starred_url":"https://api.github.com/users/rollokb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rollokb/subscriptions","organizations_url":"https://api.github.com/users/rollokb/orgs","repos_url":"https://api.github.com/users/rollokb/repos","events_url":"https://api.github.com/users/rollokb/events{/privacy}","received_events_url":"https://api.github.com/users/rollokb/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-10-29T01:01:27Z","updated_at":"2015-10-29T01:01:27Z","closed_at":null,"body":"Sometimes I need to get a relationship based from a function on a model like this.\r\n\r\n    class Post(models.Model):\r\n        # attributes and stuff\r\n       \r\n       def user_recommendation(self, user)\r\n           return self.recommends.get(user=user)\r\n\r\n In vanila DRF it is possible to use methods like `user_recommendation` by overriding `RelatedField` or using a`SerializerMethodField`.  However neither of those methods work with DRF-JSON-API. Using a `SerializerMethodField` results in the relation being placed in attributes, not relations.\r\n\r\nWhile subclassing `RelatedField` fails because at this point in the [renderer](https://github.com/django-json-api/django-rest-framework-json-api/blob/develop/rest_framework_json_api/utils.py#L325) attempts to get an id from`relation_instance_or_manager`, but will fail because it's a bound method.\r\n\r\n-----\r\n\r\nTo get the relationship of user_recommendation I have to resort to a dirty hack. Where I subclass `HyperlinkedRelatedField` or `PKRelatedField`, and override `get_attribute` like this.\r\n\r\n    class RecommendationField(serializers.HyperlinkedRelatedField):\r\n        def get_attribute(self, instance): \r\n            request = self.context['request']\r\n            recommendation = instance.user_recommendation(request.user)\r\n            instance.user_recommendation.__dict__['pk'] = recommendation.pk\r\n            return recommendation\r\n"}}