Ticket #12483: 12483.diff

File 12483.diff, 2.2 KB (added by Matthias Kestenholz, 14 years ago)
  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index 4aa6cfa..ab23be8 100644
    a b class Model(object):  
    371371            u = unicode(self)
    372372        except (UnicodeEncodeError, UnicodeDecodeError):
    373373            u = '[Bad Unicode data]'
     374        except TypeError, e:
     375            raise TypeError("%s: %s" % (self.__class__, e))
    374376        return smart_str(u'<%s: %s>' % (self.__class__.__name__, u))
    375377
    376378    def __str__(self):
  • tests/regressiontests/model_regress/models.py

    diff --git a/tests/regressiontests/model_regress/models.py b/tests/regressiontests/model_regress/models.py
    index f30b3ee..e1c03fd 100644
    a b class BrokenUnicodeMethod(models.Model):  
    5757
    5858class NonAutoPK(models.Model):
    5959    name = models.CharField(max_length=10, primary_key=True)
     60
     61class NoneUnicodeMethod(models.Model):
     62    def __unicode__(self):
     63        # Intentionally broken. Returns None
     64        return None
  • tests/regressiontests/model_regress/tests.py

    diff --git a/tests/regressiontests/model_regress/tests.py b/tests/regressiontests/model_regress/tests.py
    index a103590..877aa92 100644
    a b from django.test import TestCase, skipUnlessDBFeature  
    66from django.utils import tzinfo
    77
    88from models import (Worker, Article, Party, Event, Department,
    9     BrokenUnicodeMethod, NonAutoPK)
     9    BrokenUnicodeMethod, NonAutoPK, NoneUnicodeMethod)
    1010
    1111
    1212
    class ModelTests(TestCase):  
    145145        b = BrokenUnicodeMethod.objects.create(name="Jerry")
    146146        self.assertEqual(repr(b), "<BrokenUnicodeMethod: [Bad Unicode data]>")
    147147
     148        n = NoneUnicodeMethod()
     149        self.assertRaises(TypeError, lambda: unicode(n))
     150        self.assertRaisesRegexp(
     151            TypeError,
     152            "<class 'regressiontests.model_regress.models.NoneUnicodeMethod'>: coercing to Unicode: need string or buffer, NoneType found",
     153            lambda: repr(NoneUnicodeMethod())
     154            )
     155
    148156    @skipUnlessDBFeature("supports_timezones")
    149157    def test_timezones(self):
    150158        # Saving an updating with timezone-aware datetime Python objects.
Back to Top