Opened 12 months ago
Last modified 4 days ago
#36539 assigned Cleanup/optimization
Label for related fields should use related model verbose_name attribute for field
| Reported by: | Leandro de Souza | Owned by: | Pycon Korea Sprints |
|---|---|---|---|
| Component: | contrib.admin | Version: | dev |
| Severity: | Normal | Keywords: | list_display, fieldsets |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | yes |
| Easy pickings: | no | UI/UX: | no |
Description
Django 5.1 has added support for referencing related fields directly from the list_display/fieldsets option on the django.contrib.admin.options.ModelAdmin class (#10743).
However, when referencing this field, the label generated for this field is using the attribute name instead of the verbose_name defined on that field.
Using the verbose_name would make much more sense and would be a great addition.
Example:
# books/models.py
from django.db import models
class Author(models.Model):
name = models.CharField(verbose_name="Name of the author")
class Book(models.Model):
author = models.ForeignKey(to=Author, on_delete=models.PROTECT)
# books/admin.py
from django.contrib import admin
from .models import Book
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ("author__name",)
The above code would display on the change_list page: "Author name" instead of the verbose_name defined at the author.name field.
The same applies to fieldsets used on the change page.
Change History (10)
comment:1 by , 12 months ago
comment:2 by , 12 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Cleanup/optimization |
| Version: | 5.2 → dev |
Hello Leandro de Souza, thank you for creating this ticket. I think this is a good improvement, I'm looking forward to your contribution.
comment:3 by , 12 months ago
Fix should be along these lines:
-
django/contrib/admin/utils.py
diff --git a/django/contrib/admin/utils.py b/django/contrib/admin/utils.py index 74bd571e56..23a6b36b9c 100644
a b def label_for_field(name, model, model_admin=None, return_attr=False, form=None) 394 394 395 395 if hasattr(attr, "short_description"): 396 396 label = attr.short_description 397 elif hasattr(attr, "verbose_name"): 398 label = attr.verbose_name 397 399 elif ( 398 400 isinstance(attr, property) 399 401 and hasattr(attr, "fget")
comment:5 by , 12 months ago
Just opened the PR, probably will break some tests, will fix them some time soon.
comment:6 by , 12 months ago
Looks like all tests that should've run are ok. Let me know if any changes are needed
comment:7 by , 12 months ago
| Needs tests: | set |
|---|
comment:8 by , 12 months ago
| Needs tests: | unset |
|---|
comment:9 by , 12 months ago
| Patch needs improvement: | set |
|---|
comment:10 by , 4 days ago
| Owner: | changed from to |
|---|
Attempting to assign to Pycon Korea Sprints to make it easier to reserve tickets. Pycon Korea ends August 17, this is available for someone else to pick up on the 18th.
Just in time..
I've managed to create a monkey patched version with the changes required for this to work.
I would be happy to submit a patch for this ticket if desired, this can also include supporting casting related fields to their "choice" human readable value using .get_fieldname_display