From 3733ae895780f17430924f1e20ee320556c62d05 Mon Sep 17 00:00:00 2001 From: Raffaele Salmaso Date: Tue, 22 Sep 2020 10:46:27 +0200 Subject: [PATCH] Fixed #32031 -- Added model class for each model to AdminSite.each_context(). --- django/contrib/admin/sites.py | 1 + docs/ref/contrib/admin/index.txt | 5 +++++ docs/releases/4.0.txt | 3 +++ tests/admin_views/test_adminsite.py | 5 ++++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index 837dabfea8..59a4a0364d 100644 --- a/django/contrib/admin/sites.py +++ b/django/contrib/admin/sites.py @@ -461,6 +461,7 @@ class AdminSite: info = (app_label, model._meta.model_name) model_dict = { + 'model': model, 'name': capfirst(model._meta.verbose_name_plural), 'object_name': model._meta.object_name, 'perms': perms, diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 8da7a72f10..2e99c498e9 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -3025,6 +3025,7 @@ Templates can override or extend base admin templates as described in Each model is a dict with the following keys: + * ``model``: the model class * ``object_name``: class name of the model * ``name``: plural name of the model * ``perms``: a ``dict`` tracking ``add``, ``change``, ``delete``, and @@ -3032,6 +3033,10 @@ Templates can override or extend base admin templates as described in * ``admin_url``: admin changelist URL for the model * ``add_url``: admin URL to add a new model instance + .. versionchanged:: 4.0 + + The ``model`` variable for each model was added. + .. method:: AdminSite.has_permission(request) Returns ``True`` if the user for the given ``HttpRequest`` has permission diff --git a/docs/releases/4.0.txt b/docs/releases/4.0.txt index fdd4b6c39b..3168200070 100644 --- a/docs/releases/4.0.txt +++ b/docs/releases/4.0.txt @@ -72,6 +72,9 @@ Minor features * The navigation sidebar now has a quick filter toolbar. +* The new context variable ``model`` which contains the model class for each + model is added to the :meth:`.AdminSite.each_context` method. + :mod:`django.contrib.admindocs` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/admin_views/test_adminsite.py b/tests/admin_views/test_adminsite.py index efee6f39c6..4874bc48c8 100644 --- a/tests/admin_views/test_adminsite.py +++ b/tests/admin_views/test_adminsite.py @@ -55,7 +55,9 @@ class SiteEachContextTest(TestCase): admin_views = apps[0] self.assertEqual(admin_views['app_label'], 'admin_views') self.assertEqual(len(admin_views['models']), 1) - self.assertEqual(admin_views['models'][0]['object_name'], 'Article') + article = admin_views['models'][0] + self.assertEqual(article['object_name'], 'Article') + self.assertEqual(article['model'], Article) # auth.User auth = apps[1] @@ -63,6 +65,7 @@ class SiteEachContextTest(TestCase): self.assertEqual(len(auth['models']), 1) user = auth['models'][0] self.assertEqual(user['object_name'], 'User') + self.assertEqual(user['model'], User) self.assertEqual(auth['app_url'], '/test_admin/admin/auth/') self.assertIs(auth['has_module_perms'], True)