diff options
| author | Simon Meers <simon@simonmeers.com> | 2012-08-20 16:47:30 +1000 |
|---|---|---|
| committer | Simon Meers <simon@simonmeers.com> | 2012-08-20 16:47:30 +1000 |
| commit | 3fce0d2a9162cf6e749a6de0b18890dea8955e89 (patch) | |
| tree | 437cfd55c46576628ceb9bec789ab097d825a314 | |
| parent | 30bdf22bc7a262858b02b65b0ed154dec46d911d (diff) | |
Fixed #18063 -- Avoid unicode in Model.__repr__ in python 2
Thanks guettli and mrmachine.
| -rw-r--r-- | django/db/models/base.py | 2 | ||||
| -rw-r--r-- | tests/regressiontests/model_regress/tests.py | 10 |
2 files changed, 12 insertions, 0 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py index fd7250cdb0..d7a9932388 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -404,6 +404,8 @@ class Model(six.with_metaclass(ModelBase, object)): 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): diff --git a/tests/regressiontests/model_regress/tests.py b/tests/regressiontests/model_regress/tests.py index 6a45a83052..6a03b861e4 100644 --- a/tests/regressiontests/model_regress/tests.py +++ b/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 @@ class ModelTests(TestCase): 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. |
