Skip to content

Fixed #36539 -- Admin uses related field verbose_name attribute if available #19704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

leandrodesouzadev
Copy link
Contributor

@leandrodesouzadev leandrodesouzadev commented Aug 4, 2025

Trac ticket number

ticket-36539

Branch description

Related name supported was added on 5.1 but related field verbose names weren't taken into consideration.
This PR added support for using the verbose_name attribute of the related field.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.

Copy link
Contributor

@nessita nessita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @leandrodesouzadev for starting this work! 🌟

We should definitely add more tests ensuring that fields with and without verbose name are treated correctly. Particularly the domain field that does not define a verbose_name should still show what was shown previously.

Does that make sense?

@nessita
Copy link
Contributor

nessita commented Aug 4, 2025

Also where are the screenshots that you mentioned you took by checking the item in the PR template list?

@leandrodesouzadev
Copy link
Contributor Author

Also where are the screenshots that you mentioned you took by checking the item in the PR template list?

I thought that since there aren't any changes related to the documentation, then I could just checked them.
Since there are no changes since now, should've kept it unchecked?

@nessita
Copy link
Contributor

nessita commented Aug 4, 2025

Also where are the screenshots that you mentioned you took by checking the item in the PR template list?

I thought that since there aren't any changes related to the documentation, then I could just checked them. Since there are no changes since now, should've kept it unchecked?

I see the confusion! If you are not uploading screenshots, you can leave it unchecked or delete them. Thank you for asking!

@leandrodesouzadev
Copy link
Contributor Author

We should definitely add more tests ensuring that fields with and without verbose name are treated correctly. Particularly the domain field that does not define a verbose_name should still show what was shown previously.

Just pushed a few changes regarding the tests for using with/without verbose_name

Copy link
Contributor

@Antoliny0919 Antoliny0919 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this ticket @leandrodesouzadev ⭐️

This is definitely a necessary change and something that should be improved!

Screenshot 2025-08-05 at 12 54 38 PM

Comment on lines 349 to 352
self.assertEqual(label_for_field("site__owner", Article), owner_verbose_name)
self.assertEqual(
label_for_field("site__domain", Article, return_attr=True),
("Site domain", Site._meta.get_field("domain")),
(owner_verbose_name, domain_field),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a mistake 😁

index e2f55b80f3..8bffa26683 100644
--- a/tests/admin_utils/tests.py
+++ b/tests/admin_utils/tests.py
@@ -348,8 +348,8 @@ class UtilsTests(SimpleTestCase):
         self.assertEqual(owner_field.verbose_name, owner_verbose_name)
         self.assertEqual(label_for_field("site__owner", Article), owner_verbose_name)
         self.assertEqual(
-            label_for_field("site__domain", Article, return_attr=True),
-            (owner_verbose_name, domain_field),
+            label_for_field("site__owner", Article, return_attr=True),
+            (owner_verbose_name, owner_field),
         )
 
     def test_label_for_field_failed_lookup(self):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Was caught by the copy-pasta police. Just pushed a fix

@leandrodesouzadev
Copy link
Contributor Author

Is there any wacky tests? A test is failing without any change related to the tests/code path.

FAIL: test_logentry_change_message_formsets (admin_utils.test_logentry.LogEntryTests.test_logentry_change_message_formsets)
All messages for changed formsets are logged in a change message.

Don't really know what do next now.

@@ -5,6 +5,7 @@

class Site(models.Model):
domain = models.CharField(max_length=100)
owner = models.CharField(verbose_name=_("Site owner"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any wacky tests? A test is failing without any change related to the tests/code path.

FAIL: test_logentry_change_message_formsets (admin_utils.test_logentry.LogEntryTests.test_logentry_change_message_formsets)
All messages for changed formsets are logged in a change message.

Don't really know what do next now.

index 37d13d24bd..50023b5788 100644
--- a/tests/admin_utils/test_logentry.py
+++ b/tests/admin_utils/test_logentry.py
@@ -21,7 +21,7 @@ class LogEntryTests(TestCase):
         cls.user = User.objects.create_superuser(
             username="super", password="secret", email="super@example.com"
         )
-        cls.site = Site.objects.create(domain="example.org")
+        cls.site = Site.objects.create(domain="example.org", owner="Tom")
         cls.a1 = Article.objects.create(
             site=cls.site,
             title="Title",
@@ -148,6 +148,7 @@ class LogEntryTests(TestCase):
         )
         post_data = {
             "domain": "example.com",  # domain changed
+            "owner": "Tom",
             "admin_articles-TOTAL_FORMS": "5",
             "admin_articles-INITIAL_FORMS": "2",
             "admin_articles-MIN_NUM_FORMS": "0",

Since a field was added to the Site model, that field also needs to be included when passing data to the form.
It seems like this issue is what's causing the problem!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, thanks for pointing out.

Copy link
Contributor

@Antoliny0919 Antoliny0919 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me ⭐️
Just don’t forget to remove the ticket flags after finishing up the work!

@leandrodesouzadev
Copy link
Contributor Author

Great stuff, thanks for the help on my first PR @nessita and @Antoliny0919.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants