Django

Code

Changeset 9475

Show
Ignore:
Timestamp:
11/16/08 12:58:43 (2 months ago)
Author:
kmtracey
Message:

Fixed #9608: Ensured a Model's default repr() is printable even if its unicode method raises a Unicode error.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/base.py

    r8932 r9475  
    267267 
    268268    def __repr__(self): 
    269         return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self))) 
     269        try: 
     270            u = unicode(self) 
     271        except (UnicodeEncodeError, UnicodeDecodeError): 
     272            u = '[Bad Unicode data]' 
     273        return smart_str(u'<%s: %s>' % (self.__class__.__name__, u)) 
    270274 
    271275    def __str__(self): 
  • django/trunk/tests/regressiontests/model_regress/models.py

    r9466 r9475  
    4646    def __unicode__(self): 
    4747        return self.name 
     48 
     49class BrokenUnicodeMethod(models.Model): 
     50    name = models.CharField(max_length=7) 
     51    def __unicode__(self): 
     52        return 'Názov: %s' % self.name 
     53 
    4854 
    4955__test__ = {'API_TESTS': """ 
     
    129135<Worker: Full-time> 
    130136 
     137# Models with broken unicode methods should still have a printable repr 
     138>>> b = BrokenUnicodeMethod(name="Jerry") 
     139>>> b.save() 
     140>>> BrokenUnicodeMethod.objects.all() 
     141[<BrokenUnicodeMethod: [Bad Unicode data]>] 
     142 
    131143""" 
    132144}