From 4ade8386ebfeb7a781dc2b62542c1cf5f8b9ddaf Mon Sep 17 00:00:00 2001 From: Tom Carrick Date: Tue, 4 Apr 2023 15:11:11 +0100 Subject: Fixed #10743 -- Allowed lookups for related fields in ModelAdmin.list_display. Co-authored-by: Alex Garcia Co-authored-by: Natalia <124304+nessita@users.noreply.github.com> Co-authored-by: Nina Menezes --- tests/admin_utils/tests.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests/admin_utils') diff --git a/tests/admin_utils/tests.py b/tests/admin_utils/tests.py index 067b47198d..56d46324e0 100644 --- a/tests/admin_utils/tests.py +++ b/tests/admin_utils/tests.py @@ -137,6 +137,7 @@ class UtilsTests(SimpleTestCase): (simple_function, SIMPLE_FUNCTION), ("test_from_model", article.test_from_model()), ("non_field", INSTANCE_ATTRIBUTE), + ("site__domain", SITE_NAME), ) mock_admin = MockModelAdmin() @@ -294,6 +295,17 @@ class UtilsTests(SimpleTestCase): self.assertEqual(label_for_field(lambda x: "nothing", Article), "--") self.assertEqual(label_for_field("site_id", Article), "Site id") + # The correct name and attr are returned when `__` is in the field name. + self.assertEqual(label_for_field("site__domain", Article), "Site domain") + self.assertEqual( + label_for_field("site__domain", Article, return_attr=True), + ("Site domain", Site._meta.get_field("domain")), + ) + + def test_label_for_field_failed_lookup(self): + msg = "Unable to lookup 'site__unknown' on Article" + with self.assertRaisesMessage(AttributeError, msg): + label_for_field("site__unknown", Article) class MockModelAdmin: @admin.display(description="not Really the Model") -- cgit v1.3