Ticket #18063: no-unicode-in-repr.diff

File no-unicode-in-repr.diff, 1.5 KB (added by Thomas Güttler, 12 years ago)
  • tests/regressiontests/model_regress/tests.py

     
     1# coding: utf-8
    12from __future__ import absolute_import
    23
    34import datetime
     
    145146        # Models with broken unicode methods should still have a printable repr
    146147        b = BrokenUnicodeMethod.objects.create(name="Jerry")
    147148        self.assertEqual(repr(b), "<BrokenUnicodeMethod: [Bad Unicode data]>")
    148 
     149       
     150    def test_no_unicode_in_repr(self):
     151        a = Article.objects.create(
     152            headline=u"Watch for umlauts: üöä", pub_date=datetime.datetime.now())
     153        self.assertEqual(repr(a), '<Article: Watch for umlauts: ???>')
     154       
    149155    @skipUnlessDBFeature("supports_timezones")
    150156    def test_timezones(self):
    151157        # Saving an updating with timezone-aware datetime Python objects.
  • django/db/models/base.py

     
    373373            u = unicode(self)
    374374        except (UnicodeEncodeError, UnicodeDecodeError):
    375375            u = '[Bad Unicode data]'
    376         return smart_str(u'<%s: %s>' % (self.__class__.__name__, u))
     376        return smart_str(u'<%s: %s>' % (self.__class__.__name__, u.encode('ascii', 'replace')))
    377377
    378378    def __str__(self):
    379379        if hasattr(self, '__unicode__'):
Back to Top