Skip to content

Commit

Permalink
Refs #23919 -- Removed support for broken Model.__str__() in Model.__…
Browse files Browse the repository at this point in the history
…repr__().

Returning invalid bytestrings in __str__() is unlikely in Python 3.
  • Loading branch information
timgraham committed Jun 11, 2017
1 parent 3eb3907 commit 3d0a0ec
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 20 deletions.
6 changes: 1 addition & 5 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,7 @@ def from_db(cls, db, field_names, values):
return new

def __repr__(self):
try:
u = str(self)
except (UnicodeEncodeError, UnicodeDecodeError):
u = '[Bad Unicode data]'
return '<%s: %s>' % (self.__class__.__name__, u)
return '<%s: %s>' % (self.__class__.__name__, self)

def __str__(self):
return '%s object (%s)' % (self.__class__.__name__, self.pk)
Expand Down
8 changes: 0 additions & 8 deletions tests/model_regress/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ def __str__(self):
return self.name


class BrokenStrMethod(models.Model):
name = models.CharField(max_length=7)

def __str__(self):
# Intentionally broken (invalid start byte in byte string).
return b'Name\xff: %s'.decode() % self.name


class NonAutoPK(models.Model):
name = models.CharField(max_length=10, primary_key=True)

Expand Down
9 changes: 2 additions & 7 deletions tests/model_regress/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from django.utils.timezone import get_fixed_timezone

from .models import (
Article, BrokenStrMethod, Department, Event, Model1, Model2, Model3,
NonAutoPK, Party, Worker,
Article, Department, Event, Model1, Model2, Model3, NonAutoPK, Party,
Worker,
)


Expand Down Expand Up @@ -186,11 +186,6 @@ def test_primary_key_foreign_key_types(self):
w = Worker.objects.create(department=d, name="Full-time")
self.assertEqual(str(w), "Full-time")

def test_broken_unicode(self):
# Models with broken __str__() methods have a printable repr().
b = BrokenStrMethod.objects.create(name='Jerry')
self.assertEqual(repr(b), '<BrokenStrMethod: [Bad Unicode data]>')

@skipUnlessDBFeature("supports_timezones")
def test_timezones(self):
# Saving an updating with timezone-aware datetime Python objects.
Expand Down

0 comments on commit 3d0a0ec

Please sign in to comment.