Fixed #10743 -- Allowed lookups for related fields in ModelAdmin.list_display.

Co-authored-by: Alex Garcia <me@alexoteiza.com>
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Co-authored-by: Nina Menezes <https://github.com/nmenezes0>
This commit is contained in:
Tom Carrick 2023-04-04 15:11:11 +01:00 committed by Natalia
parent 3580b47ed3
commit 4ade8386eb
13 changed files with 186 additions and 46 deletions

View file

@ -726,9 +726,9 @@ with the admin site:
* **admin.E106**: The value of ``<InlineModelAdmin class>.model`` must be a
``Model``.
* **admin.E107**: The value of ``list_display`` must be a list or tuple.
* **admin.E108**: The value of ``list_display[n]`` refers to ``<label>``,
which is not a callable, an attribute of ``<ModelAdmin class>``, or an
attribute or method on ``<model>``.
* **admin.E108**: The value of ``list_display[n]`` refers to ``<label>``, which
is not a callable or attribute of ``<ModelAdmin class>``, or an attribute,
method, or field on ``<model>``.
* **admin.E109**: The value of ``list_display[n]`` must not be a many-to-many
field or a reverse foreign key.
* **admin.E110**: The value of ``list_display_links`` must be a list, a tuple,

View file

@ -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.