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 %}
<br>
{% 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 %}
<span class="mini quiet">{% trans 'Unknown content' %}</span>
{% endif %}

View File

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

View File

@ -142,7 +142,7 @@ class ContentType(models.Model):
unique_together = (('app_label', 'model'),)
def __str__(self):
return self.name
return self.app_labeled_name
@property
def name(self):
@ -151,6 +151,13 @@ class ContentType(models.Model):
return self.model
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):
"""Return the model class for this type of content."""
try:

View File

@ -285,7 +285,8 @@ Django 3.0, we're removing these APIs at this time.
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:

View File

@ -201,7 +201,15 @@ class ContentTypesTests(TestCase):
def test_str(self):
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:

View File

@ -20,6 +20,6 @@ class ContentTypeTests(TestCase):
def test_verbose_name(self):
company_type = ContentType.objects.get(app_label='i18n', model='company')
with translation.override('en'):
self.assertEqual(str(company_type), 'Company')
self.assertEqual(str(company_type), 'i18n | Company')
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
msg = (
'Cannot assign "<ContentType: book>": the current database router '
'prevents this relation.'
'Cannot assign "<ContentType: multiple_database | book>": the '
'current database router prevents this relation.'
)
with self.assertRaisesMessage(ValueError, msg):
review1.content_object = dive