Fixed #31558 -- Added support for boolean attribute on properties in ModelAdmin.list_display.

This commit is contained in:
Ryanwalker277 2023-09-14 17:40:20 +05:30 committed by Mariusz Felisiak
parent 2f1ab16be5
commit 225328efd9
7 changed files with 29 additions and 0 deletions

View File

@ -225,6 +225,9 @@ def items_for_result(cl, result, form):
if field_name == "action_checkbox": if field_name == "action_checkbox":
row_classes = ["action-checkbox"] row_classes = ["action-checkbox"]
boolean = getattr(attr, "boolean", False) boolean = getattr(attr, "boolean", False)
# Set boolean for attr that is a property, if defined.
if isinstance(attr, property) and hasattr(attr, "fget"):
boolean = getattr(attr.fget, "boolean", False)
result_repr = display_for_value(value, empty_value_display, boolean) result_repr = display_for_value(value, empty_value_display, boolean)
if isinstance(value, (datetime.date, datetime.time)): if isinstance(value, (datetime.date, datetime.time)):
row_classes.append("nowrap") row_classes.append("nowrap")

View File

@ -302,6 +302,8 @@ def lookup_field(name, obj, model_admin=None):
value = attr() value = attr()
else: else:
value = attr value = attr
if hasattr(model_admin, "model") and hasattr(model_admin.model, name):
attr = getattr(model_admin.model, name)
f = None f = None
else: else:
attr = None attr = None

View File

@ -796,6 +796,7 @@ subclass::
@admin.display( @admin.display(
ordering="last_name", ordering="last_name",
description="Full name of the person", description="Full name of the person",
boolean=False,
) )
def full_name(self): def full_name(self):
return self.first_name + " " + self.last_name return self.first_name + " " + self.last_name
@ -816,9 +817,14 @@ subclass::
my_property.short_description = "Full name of the person" my_property.short_description = "Full name of the person"
my_property.admin_order_field = "last_name" my_property.admin_order_field = "last_name"
my_property.boolean = False
full_name = property(my_property) full_name = property(my_property)
.. versionchanged:: 5.0
Support for ``boolean`` attribute on properties was added.
* The field names in ``list_display`` will also appear as CSS classes in * The field names in ``list_display`` will also appear as CSS classes in
the HTML output, in the form of ``column-<field_name>`` on each ``<th>`` the HTML output, in the form of ``column-<field_name>`` on each ``<th>``
element. This can be used to set column widths in a CSS file for example. element. This can be used to set column widths in a CSS file for example.

View File

@ -211,6 +211,9 @@ Minor features
* The new :meth:`.AdminSite.get_model_admin` method returns an admin class for * The new :meth:`.AdminSite.get_model_admin` method returns an admin class for
the given model class. the given model class.
* Properties in :attr:`.ModelAdmin.list_display` now support ``boolean``
attribute.
:mod:`django.contrib.admindocs` :mod:`django.contrib.admindocs`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -216,6 +216,7 @@ class ArticleAdmin(ArticleAdminWithExtraUrl):
"model_month", "model_month",
"order_by_f_expression", "order_by_f_expression",
"order_by_orderby_expression", "order_by_orderby_expression",
"model_property_is_from_past",
) )
list_editable = ("section",) list_editable = ("section",)
list_filter = ("date", "section") list_filter = ("date", "section")

View File

@ -9,6 +9,7 @@ from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.files.storage import FileSystemStorage from django.core.files.storage import FileSystemStorage
from django.db import models from django.db import models
from django.utils import timezone
class Section(models.Model): class Section(models.Model):
@ -66,6 +67,11 @@ class Article(models.Model):
def model_month(self): def model_month(self):
return self.date.month return self.date.month
@property
@admin.display(description="Is from past?", boolean=True)
def model_property_is_from_past(self):
return self.date < timezone.now()
class Book(models.Model): class Book(models.Model):
""" """

View File

@ -486,6 +486,14 @@ class AdminViewBasicTest(AdminViewBasicTestCase):
"Results of sorting on callable are out of order.", "Results of sorting on callable are out of order.",
) )
def test_change_list_boolean_display_property(self):
response = self.client.get(reverse("admin:admin_views_article_changelist"))
self.assertContains(
response,
'<td class="field-model_property_is_from_past">'
'<img src="/static/admin/img/icon-yes.svg" alt="True"></td>',
)
def test_change_list_sorting_property(self): def test_change_list_sorting_property(self):
""" """
Sort on a list_display field that is a property (column 10 is Sort on a list_display field that is a property (column 10 is