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 --- docs/ref/contrib/admin/index.txt | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'docs/ref/contrib/admin') diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index e85ba9c36a..e0e57b9fc0 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -315,9 +315,9 @@ subclass:: For more complex layout needs, see the :attr:`~ModelAdmin.fieldsets` option. The ``fields`` option accepts the same types of values as - :attr:`~ModelAdmin.list_display`, except that callables aren't accepted. - Names of model and model admin methods will only be used if they're listed - in :attr:`~ModelAdmin.readonly_fields`. + :attr:`~ModelAdmin.list_display`, except that callables and ``__`` lookups + for related fields aren't accepted. Names of model and model admin methods + will only be used if they're listed in :attr:`~ModelAdmin.readonly_fields`. To display multiple fields on the same line, wrap those fields in their own tuple. In this example, the ``url`` and ``title`` fields will display on the @@ -565,7 +565,7 @@ subclass:: If you don't set ``list_display``, the admin site will display a single column that displays the ``__str__()`` representation of each object. - There are four types of values that can be used in ``list_display``. All + There are five types of values that can be used in ``list_display``. All but the simplest may use the :func:`~django.contrib.admin.display` decorator, which is used to customize how the field is presented: @@ -574,6 +574,11 @@ subclass:: class PersonAdmin(admin.ModelAdmin): list_display = ["first_name", "last_name"] + * The name of a related field, using the ``__`` notation. For example:: + + class PersonAdmin(admin.ModelAdmin): + list_display = ["city__name"] + * A callable that accepts one argument, the model instance. For example:: @admin.display(description="Name") @@ -614,6 +619,11 @@ subclass:: class PersonAdmin(admin.ModelAdmin): list_display = ["name", "decade_born_in"] + .. versionchanged:: 5.1 + + Support for using ``__`` lookups was added, when targeting related + fields. + A few special cases to note about ``list_display``: * If the field is a ``ForeignKey``, Django will display the @@ -831,7 +841,7 @@ subclass:: * Django will try to interpret every element of ``list_display`` in this order: - * A field of the model. + * A field of the model or from a related field. * A callable. * A string representing a ``ModelAdmin`` attribute. * A string representing a model attribute. -- cgit v1.3