Repository: encode/django-rest-framework

Issue:
PKOnlyObject doesn't have __str__/__unicode__ methods
## Checklist
- [ x] I have verified that that issue exists against the `master` branch of Django REST framework.
- [x ] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- [ x] This is not a usage question. (Those should be directed to the [discussion group](https://groups.google.com/forum/#!forum/django-rest-framework) instead.)
- [x ] This cannot be dealt with as a third party library. (We prefer new functionality to be [in the form of third party libraries](http://www.django-rest-framework.org/topics/third-party-resources/#about-third-party-packages) where possible.)
- [x ] I have reduced the issue to the simplest possible case.
- [x ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
## Steps to reproduce

Select admin renderer. View a model that has a ForeignKey like `origin = models.ForeignKey(Address, related_name='+')`  See gibberish output like `<rest_framework.relations.PKOnlyObject object at 0x7f943b762950>`
## Expected behavior

my-pretty-pk-uuid-string
## Actual behavior

`<rest_framework.relations.PKOnlyObject object at 0x7f9a6903d510>`

Should be fairly simple to add **str**/**unicode** methods so the link text is the pk value instead of something like `<rest_framework.relations.PKOnlyObject object at 0x7f9a6903d510>.`

This simple monkey patch fixes the issue so I assume adding the method won't cause any problems

```
from rest_framework.relations import PKOnlyObject

PKOnlyObject.__str__ = lambda obj: str(obj.pk)

```



Discussion:
Could you add a screenshot so we get the big picture ?

```
PKOnlyObject.__str__ = lambda obj: str(obj.pk)
PKOnlyObject.__unicode__ = lambda obj: text_type(obj.pk)
```

no monkeypatch:

<img width="959" alt="drf-issue-4419-no-monkeypatch" src="https://cloud.githubusercontent.com/assets/77731/17809717/989067d6-65e6-11e6-91b7-0b56a2c38db6.png">

has monkeypatch:

<img width="960" alt="drf-issue-4419-has-monkeypatch" src="https://cloud.githubusercontent.com/assets/77731/17809731/a4bcd74c-65e6-11e6-803c-03b5f63b976e.png">


Resolving patch:
diff --git a/rest_framework/relations.py b/rest_framework/relations.py
--- a/rest_framework/relations.py
+++ b/rest_framework/relations.py
@@ -10,7 +10,7 @@
 from django.db.models import Manager
 from django.db.models.query import QuerySet
 from django.utils import six
-from django.utils.encoding import smart_text
+from django.utils.encoding import python_2_unicode_compatible, smart_text
 from django.utils.six.moves.urllib import parse as urlparse
 from django.utils.translation import ugettext_lazy as _
 
@@ -47,6 +47,7 @@ def __getnewargs__(self):
     is_hyperlink = True
 
 
+@python_2_unicode_compatible
 class PKOnlyObject(object):
     """
     This is a mock object, used for when we only need the pk of the object
@@ -56,6 +57,9 @@ class PKOnlyObject(object):
     def __init__(self, pk):
         self.pk = pk
 
+    def __str__(self):
+        return "%s" % self.pk
+
 
 # We assume that 'validators' are intended for the child serializer,
 # rather than the parent serializer.


Test patch: