Commits

Anonymous committed 1946d62

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

r9475 from trunk.

  • Participants
  • Parent commits 441697a
  • Branches releases/1.0.X

Comments (0)

Files changed (2)

django/db/models/base.py

         signals.post_init.send(sender=self.__class__, instance=self)
 
     def __repr__(self):
-        return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))
+        try:
+            u = unicode(self)
+        except (UnicodeEncodeError, UnicodeDecodeError):
+            u = '[Bad Unicode data]'
+        return smart_str(u'<%s: %s>' % (self.__class__.__name__, u))
 
     def __str__(self):
         if hasattr(self, '__unicode__'):

tests/regressiontests/model_regress/models.py

     def __unicode__(self):
         return self.name
 
+class BrokenUnicodeMethod(models.Model):
+    name = models.CharField(max_length=7)
+    def __unicode__(self):
+        return 'Názov: %s' % self.name
+
+
 __test__ = {'API_TESTS': """
 (NOTE: Part of the regression test here is merely parsing the model
 declaration. The verbose_name, in particular, did not always work.)
 >>> w
 <Worker: Full-time>
 
+# Models with broken unicode methods should still have a printable repr
+>>> b = BrokenUnicodeMethod(name="Jerry")
+>>> b.save()
+>>> BrokenUnicodeMethod.objects.all()
+[<BrokenUnicodeMethod: [Bad Unicode data]>]
+
 """
 }