Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9608: Ensured a Model's default repr() is printable even if it…

…s __unicode__ method raises a Unicode error.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9475 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8e350d036ce6e12b2c2e9229c6b00d4da7823ffb 1 parent bcb4693
Karen Tracey authored November 16, 2008
6  django/db/models/base.py
@@ -266,7 +266,11 @@ def __init__(self, *args, **kwargs):
266 266
         signals.post_init.send(sender=self.__class__, instance=self)
267 267
 
268 268
     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))
270 274
 
271 275
     def __str__(self):
272 276
         if hasattr(self, '__unicode__'):
12  tests/regressiontests/model_regress/models.py
@@ -46,6 +46,12 @@ class Worker(models.Model):
46 46
     def __unicode__(self):
47 47
         return self.name
48 48
 
  49
+class BrokenUnicodeMethod(models.Model):
  50
+    name = models.CharField(max_length=7)
  51
+    def __unicode__(self):
  52
+        return 'Názov: %s' % self.name
  53
+
  54
+
49 55
 __test__ = {'API_TESTS': """
50 56
 (NOTE: Part of the regression test here is merely parsing the model
51 57
 declaration. The verbose_name, in particular, did not always work.)
@@ -128,5 +134,11 @@ def __unicode__(self):
128 134
 >>> w
129 135
 <Worker: Full-time>
130 136
 
  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
+
131 143
 """
132 144
 }

0 notes on commit 8e350d0

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