diff options
author | Biel Frontera <bfrontera@socib.es> | 2022-03-14 09:53:36 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-03-14 12:52:26 +0100 |
commit | 859a87d873ce7152af73ab851653b4e1c3ffea4c (patch) | |
tree | 72cda8bf81eb3bbd3e104850f72e6a145c9008fe /tests/contenttypes_tests | |
parent | 839d403e50eafa49a7e4a5a13ad818844ff9305b (diff) | |
download | django-859a87d873ce7152af73ab851653b4e1c3ffea4c.tar.gz |
Fixed #31357 -- Fixed get_for_models() crash for stale content types when model with the same name exists in another app.
Diffstat (limited to 'tests/contenttypes_tests')
-rw-r--r-- | tests/contenttypes_tests/test_models.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/tests/contenttypes_tests/test_models.py b/tests/contenttypes_tests/test_models.py index e2fb7fe818..a96e12e69f 100644 --- a/tests/contenttypes_tests/test_models.py +++ b/tests/contenttypes_tests/test_models.py @@ -248,6 +248,26 @@ class ContentTypesTests(TestCase): ct_fetched = ContentType.objects.get_for_id(ct.pk) self.assertIsNone(ct_fetched.model_class()) + def test_missing_model_with_existing_model_name(self): + """ + 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, even if a model with the same name exists in another app. + """ + # Create a stale ContentType that matches the name of an existing + # model. + ContentType.objects.create(app_label="contenttypes", model="author") + ContentType.objects.clear_cache() + # get_for_models() should work as expected for existing models. + cts = ContentType.objects.get_for_models(ContentType, Author) + self.assertEqual( + cts, + { + ContentType: ContentType.objects.get_for_model(ContentType), + Author: ContentType.objects.get_for_model(Author), + }, + ) + def test_str(self): ct = ContentType.objects.get(app_label="contenttypes_tests", model="site") self.assertEqual(str(ct), "contenttypes_tests | site") |