Opened 5 years ago

Closed 5 years ago

#30051 closed New feature (duplicate)

ContentType __str__ representation only shows model without the app that the model comes from

Reported by: Greg Schmit Owned by: nobody
Component: contrib.contenttypes Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: yes UI/UX: no


So I actually tried to fix this myself and will continue to do so, but I wanted to open a ticket to see if anyone else sees this as a problem because my simple fix broke some tests.

I noticed a dissimilarity in how Django handles models from different apps. In Permissions, for example, they are rendered like this:

admin | admin :: log entry | Can add log entry

Which is great since I can distinguish between models of different apps that might have the same name. However, when looking at a ContentType, it just shows the verbose_name of the model, like:

log entry

So if I have two apps that have models that have the same name, then they would be indistinguishable. This is a problem with ForeignKeyFields or ManyToManyFields which use ContentTypes.

A simple way to fix this behavior is to fix the __str__() method on the ContentType model. I noticed that it uses a helper name() method, so I decided to make the change there:

    def __str__(self):

    def name(self):
        model = self.model_class()
        if not model:
            return self.model
-       return str(model._meta.verbose_name)
+       return "{0} :: {1}".format(model._meta.app_label, model._meta.verbose_name)

I used the :: separator to keep convention with the Permissions style.

That made some tests fail, so I tried to clone Django on my MBP and in an Ubuntu VM, and when I ran tests, they failed even with no changes. In my Mac environment, I got weird errors like this:

+[__NSPlaceholderDate initialize] may have been in progress in another thread when fork() was called.
objc[11493]: +[__NSPlaceholderDate initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.

And also, I got an alert that "Python quit unexpectedly" and then everything crashed.

On Ubuntu, I got some "E" results, and at the end I got a bunch of errors like this:

ERROR: test_remove_constraints_capital_letters (schema.tests.SchemaTests)
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/", line 59, in testPartExecutor
  File "/usr/lib/python3.6/unittest/", line 608, in run
  File "/home/gns/django/django/tests/schema/", line 70, in tearDown
  File "/home/gns/django/django/tests/schema/", line 97, in delete_tables
  File "/home/gns/django/django/django/db/backends/sqlite3/", line 34, in __exit__
  File "/home/gns/django/django/django/db/backends/sqlite3/", line 320, in check_constraints
    column_name, referenced_column_name,
  File "/home/gns/django/django/django/db/backends/", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/gns/django/django/django/db/backends/", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/gns/django/django/django/db/backends/", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/gns/django/django/django/db/", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/gns/django/django/django/db/backends/", line 82, in _execute
    return self.cursor.execute(sql)
  File "/home/gns/django/django/django/db/backends/sqlite3/", line 360, in execute
    return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: no such table: schema_tagm2mtest

And finally in Ubuntu:

Ran 12509 tests in 422.036s

FAILED (errors=54, skipped=859, expected failures=4)

So ultimately, my goal is to have Django show ContentType objects in a similar way to how the permissions are shown, where the app that the model belongs to is available.

I'm currently thinking that maybe doing this directly in __str__ might avoid issues, and I was going to go look at the specific test cases and adjust them with this new feature, however I cannot do that since I cannot get tests to pass on a clean clone of Django. My fork is here:

Change History (3)

comment:1 by Greg Schmit, 5 years ago

Has patch: set
Needs tests: set
Patch needs improvement: set

comment:2 by Greg Schmit, 5 years ago

Version: 2.1master

comment:3 by Tim Graham, 5 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #16027.

Note: See TracTickets for help on using tickets.
Back to Top