Fixed #16027 -- Added app_label to ContentType.__str__().

This commit is contained in:
Gregory N. Schmit 2018-12-21 01:24:04 -06:00 committed by Tim Graham
parent 5cc6f02f91
commit 48c17807a9
7 changed files with 25 additions and 13 deletions

View File

@ -73,7 +73,7 @@
{% endif %} {% endif %}
<br> <br>
{% if entry.content_type %} {% if entry.content_type %}
<span class="mini quiet">{% filter capfirst %}{{ entry.content_type }}{% endfilter %}</span> <span class="mini quiet">{% filter capfirst %}{{ entry.content_type.name }}{% endfilter %}</span>
{% else %} {% else %}
<span class="mini quiet">{% trans 'Unknown content' %}</span> <span class="mini quiet">{% trans 'Unknown content' %}</span>
{% endif %} {% endif %}

View File

@ -71,11 +71,7 @@ class Permission(models.Model):
'codename') 'codename')
def __str__(self): def __str__(self):
return "%s | %s | %s" % ( return '%s | %s' % (self.content_type, self.name)
self.content_type.app_label,
self.content_type,
self.name,
)
def natural_key(self): def natural_key(self):
return (self.codename,) + self.content_type.natural_key() return (self.codename,) + self.content_type.natural_key()

View File

@ -142,7 +142,7 @@ class ContentType(models.Model):
unique_together = (('app_label', 'model'),) unique_together = (('app_label', 'model'),)
def __str__(self): def __str__(self):
return self.name return self.app_labeled_name
@property @property
def name(self): def name(self):
@ -151,6 +151,13 @@ class ContentType(models.Model):
return self.model return self.model
return str(model._meta.verbose_name) return str(model._meta.verbose_name)
@property
def app_labeled_name(self):
model = self.model_class()
if not model:
return self.model
return '%s | %s' % (model._meta.app_label, model._meta.verbose_name)
def model_class(self): def model_class(self):
"""Return the model class for this type of content.""" """Return the model class for this type of content."""
try: try:

View File

@ -285,7 +285,8 @@ Django 3.0, we're removing these APIs at this time.
Miscellaneous Miscellaneous
------------- -------------
* ... * ``ContentType.__str__()`` now includes the model's ``app_label`` to
disambiguate model's with the same name in different apps.
.. _deprecated-features-3.0: .. _deprecated-features-3.0:

View File

@ -201,7 +201,15 @@ class ContentTypesTests(TestCase):
def test_str(self): def test_str(self):
ct = ContentType.objects.get(app_label='contenttypes_tests', model='site') ct = ContentType.objects.get(app_label='contenttypes_tests', model='site')
self.assertEqual(str(ct), 'site') self.assertEqual(str(ct), 'contenttypes_tests | site')
def test_app_labeled_name(self):
ct = ContentType.objects.get(app_label='contenttypes_tests', model='site')
self.assertEqual(ct.app_labeled_name, 'contenttypes_tests | site')
def test_app_labeled_name_unknown_model(self):
ct = ContentType(app_label='contenttypes_tests', model='unknown')
self.assertEqual(ct.app_labeled_name, 'unknown')
class TestRouter: class TestRouter:

View File

@ -20,6 +20,6 @@ class ContentTypeTests(TestCase):
def test_verbose_name(self): def test_verbose_name(self):
company_type = ContentType.objects.get(app_label='i18n', model='company') company_type = ContentType.objects.get(app_label='i18n', model='company')
with translation.override('en'): with translation.override('en'):
self.assertEqual(str(company_type), 'Company') self.assertEqual(str(company_type), 'i18n | Company')
with translation.override('fr'): with translation.override('fr'):
self.assertEqual(str(company_type), 'Société') self.assertEqual(str(company_type), 'i18n | Société')

View File

@ -840,8 +840,8 @@ class QueryTestCase(TestCase):
# Set a foreign key with an object from a different database # Set a foreign key with an object from a different database
msg = ( msg = (
'Cannot assign "<ContentType: book>": the current database router ' 'Cannot assign "<ContentType: multiple_database | book>": the '
'prevents this relation.' 'current database router prevents this relation.'
) )
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
review1.content_object = dive review1.content_object = dive