Opened 10 hours ago

Last modified 6 hours 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: Leandro de Souza
Component: contrib.admin Version: dev
Severity: Normal Keywords: list_display, fieldsets
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
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 (7)

comment:1 by Leandro de Souza, 9 hours ago

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

comment:2 by Natalia Bidart, 9 hours ago

Owner: set to Leandro de Souza
Status: newassigned
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization
Version: 5.2dev

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 Natalia Bidart, 8 hours 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)  
    394394
    395395            if hasattr(attr, "short_description"):
    396396                label = attr.short_description
     397            elif hasattr(attr, "verbose_name"):
     398                label = attr.verbose_name
    397399            elif (
    398400                isinstance(attr, property)
    399401                and hasattr(attr, "fget")

comment:4 by Leandro de Souza, 8 hours ago

Has patch: set

comment:5 by Leandro de Souza, 8 hours ago

Just opened the PR, probably will break some tests, will fix them some time soon.

comment:6 by Leandro de Souza, 6 hours ago

Looks like all tests that should've run are ok. Let me know if any changes are needed

comment:7 by Natalia Bidart, 6 hours ago

Needs tests: set
Note: See TracTickets for help on using tickets.
Back to Top