Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PKOnlyObject doesn't have __str__/__unicode__ methods #4419

Closed
thenewguy opened this issue Aug 18, 2016 · 2 comments
Closed

PKOnlyObject doesn't have __str__/__unicode__ methods #4419

thenewguy opened this issue Aug 18, 2016 · 2 comments
Labels
Milestone

Comments

@thenewguy
Copy link

thenewguy commented Aug 18, 2016

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 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 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)

@xordoquy
Copy link
Collaborator

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

@thenewguy
Copy link
Author

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

no monkeypatch:

drf-issue-4419-no-monkeypatch

has monkeypatch:

drf-issue-4419-has-monkeypatch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants