Fixed #16917 -- Don't try to use the model name for a ContentType's unicode representation if the model no longer exists. Thanks Ivan Sagalaev for report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16895 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Carl Meyer 2011-09-23 04:22:30 +00:00
parent 4dab2d2f6b
commit 0a9c52e013
2 changed files with 16 additions and 3 deletions

View File

@ -92,11 +92,11 @@ class ContentType(models.Model):
# #
# We return self.name only when users have changed its value from the # We return self.name only when users have changed its value from the
# initial verbose_name_raw and might rely on it. # initial verbose_name_raw and might rely on it.
meta = self.model_class()._meta model = self.model_class()
if self.name != meta.verbose_name_raw: if not model or self.name != model._meta.verbose_name_raw:
return self.name return self.name
else: else:
return force_unicode(meta.verbose_name) return force_unicode(model._meta.verbose_name)
def model_class(self): def model_class(self):
"Returns the Python model class for this type of content." "Returns the Python model class for this type of content."

View File

@ -109,3 +109,16 @@ class ContentTypesTests(TestCase):
obj = FooWithoutUrl.objects.create(name="john") obj = FooWithoutUrl.objects.create(name="john")
self.assertRaises(Http404, shortcut, request, user_ct.id, obj.id) self.assertRaises(Http404, shortcut, request, user_ct.id, obj.id)
def test_missing_model(self):
"""
Ensures that displaying content types in admin (or anywhere) doesn't
break on leftover content type records in the DB for which no model
is defined anymore.
"""
ct = ContentType.objects.create(
name = 'Old model',
app_label = 'contenttypes',
model = 'OldModel',
)
self.assertEqual(unicode(ct), u'Old model')