Refs #24121 -- Added __repr__() to AdminSite, DefaultAdminSite, and ModelAdmin.

Thanks tlebrize for the initial patch.
This commit is contained in:
Eduardo Aldair Ahumada Garcia Jurado 2021-07-05 00:07:10 -05:00 committed by Mariusz Felisiak
parent fa35c8bdbc
commit e4da365436
4 changed files with 31 additions and 0 deletions

View File

@ -594,6 +594,12 @@ class ModelAdmin(BaseModelAdmin):
def __str__(self): def __str__(self):
return "%s.%s" % (self.model._meta.app_label, self.__class__.__name__) return "%s.%s" % (self.model._meta.app_label, self.__class__.__name__)
def __repr__(self):
return (
f'<{self.__class__.__qualname__}: model={self.model.__qualname__} '
f'site={self.admin_site!r}>'
)
def get_inline_instances(self, request, obj=None): def get_inline_instances(self, request, obj=None):
inline_instances = [] inline_instances = []
for inline_class in self.get_inlines(request, obj): for inline_class in self.get_inlines(request, obj):

View File

@ -77,6 +77,9 @@ class AdminSite:
self._global_actions = self._actions.copy() self._global_actions = self._actions.copy()
all_sites.add(self) all_sites.add(self)
def __repr__(self):
return f'{self.__class__.__name__}(name={self.name!r})'
def check(self, app_configs): def check(self, app_configs):
""" """
Run the system checks on all ModelAdmins, except if they aren't Run the system checks on all ModelAdmins, except if they aren't
@ -561,6 +564,9 @@ class DefaultAdminSite(LazyObject):
AdminSiteClass = import_string(apps.get_app_config('admin').default_site) AdminSiteClass = import_string(apps.get_app_config('admin').default_site)
self._wrapped = AdminSiteClass() self._wrapped = AdminSiteClass()
def __repr__(self):
return repr(self._wrapped)
# This global object represents the default admin site, for the common case. # This global object represents the default admin site, for the common case.
# You can provide your own AdminSite using the (Simple)AdminConfig.default_site # You can provide your own AdminSite using the (Simple)AdminConfig.default_site

View File

@ -2,6 +2,8 @@ from django.contrib import admin
from django.contrib.admin import sites from django.contrib.admin import sites
from django.test import SimpleTestCase, override_settings from django.test import SimpleTestCase, override_settings
from .sites import CustomAdminSite
@override_settings(INSTALLED_APPS=[ @override_settings(INSTALLED_APPS=[
'admin_default_site.apps.MyCustomAdminConfig', 'admin_default_site.apps.MyCustomAdminConfig',
@ -29,3 +31,13 @@ class CustomAdminSiteTests(SimpleTestCase):
class DefaultAdminSiteTests(SimpleTestCase): class DefaultAdminSiteTests(SimpleTestCase):
def test_use_default_admin_site(self): def test_use_default_admin_site(self):
self.assertEqual(admin.site.__class__.__name__, 'AdminSite') self.assertEqual(admin.site.__class__.__name__, 'AdminSite')
def test_repr(self):
self.assertEqual(str(admin.site), "AdminSite(name='admin')")
self.assertEqual(repr(admin.site), "AdminSite(name='admin')")
class AdminSiteTests(SimpleTestCase):
def test_repr(self):
admin_site = CustomAdminSite(name='other')
self.assertEqual(repr(admin_site), "CustomAdminSite(name='other')")

View File

@ -722,6 +722,13 @@ class ModelAdminTests(TestCase):
self.assertEqual(perms_needed, {'band'}) self.assertEqual(perms_needed, {'band'})
self.assertEqual(protected, []) self.assertEqual(protected, [])
def test_modeladmin_repr(self):
ma = ModelAdmin(Band, self.site)
self.assertEqual(
repr(ma),
"<ModelAdmin: model=Band site=AdminSite(name='admin')>",
)
class ModelAdminPermissionTests(SimpleTestCase): class ModelAdminPermissionTests(SimpleTestCase):