Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #18063 -- Avoid unicode in Model.__repr__ in python 2

Thanks guettli and mrmachine.
  • Loading branch information...
commit 3fce0d2a9162cf6e749a6de0b18890dea8955e89 1 parent 30bdf22
@DrMeers DrMeers authored
View
2  django/db/models/base.py
@@ -404,6 +404,8 @@ def __repr__(self):
u = six.text_type(self)
except (UnicodeEncodeError, UnicodeDecodeError):
u = '[Bad Unicode data]'
+ if not six.PY3:
+ u = u.encode('ascii', 'replace')
return smart_str('<%s: %s>' % (self.__class__.__name__, u))
def __str__(self):
View
10 tests/regressiontests/model_regress/tests.py
@@ -1,3 +1,5 @@
+# coding: utf-8
+
from __future__ import absolute_import, unicode_literals
import datetime
@@ -146,6 +148,14 @@ def test_broken_unicode(self):
b = BrokenUnicodeMethod.objects.create(name="Jerry")
self.assertEqual(repr(b), "<BrokenUnicodeMethod: [Bad Unicode data]>")
+ def test_no_unicode_in_repr(self):
+ a = Article.objects.create(
+ headline="Watch for umlauts: üöä", pub_date=datetime.datetime.now())
+ if six.PY3:
+ self.assertEqual(repr(a), '<Article: Watch for umlauts: üöä>')
+ else:
+ self.assertEqual(repr(a), '<Article: Watch for umlauts: ???>')
+
@skipUnlessDBFeature("supports_timezones")
def test_timezones(self):
# Saving an updating with timezone-aware datetime Python objects.

2 comments on commit 3fce0d2

@kmtracey
Collaborator

I do not believe this change is correct, please see: https://code.djangoproject.com/ticket/18063#comment:8

@DrMeers
Collaborator

Agreed and reverted, thanks Karen.

Please sign in to comment.
Something went wrong with that request. Please try again.