Skip to content

PKOnlyObject doesn't have __str__/__unicode__ methods #4419

@thenewguy

Description

@thenewguy

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions