Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions django/contrib/admin/templatetags/admin_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def link_in_col(is_first, field_name, cl):
empty_value_display = getattr(
attr, "empty_value_display", empty_value_display
)
# Find boolean fields on relations.
# Find a terminal field from a chain of relations.
if f is None and isinstance(field_name, str) and LOOKUP_SEP in field_name:
try:
f = get_fields_from_path(cl.model, field_name)[-1]
Expand All @@ -246,11 +246,10 @@ def link_in_col(is_first, field_name, cl):
row_classes.append("nowrap")
else:
if isinstance(f.remote_field, models.ManyToOneRel):
field_val = getattr(result, f.name)
if field_val is None:
if value is None:
result_repr = empty_value_display
else:
result_repr = field_val
result_repr = value
else:
result_repr = display_for_field(
value,
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/6.1.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ Bugfixes
* Fixed a regression in Django 6.1 where the deprecation of double-dot variable
lookups incorrectly applied to string and translated template literals
containing two consecutive dots, such as ``{{ "a..b" }}`` (:ticket:`37257`).

* Fixed a regression in Django 6.1 where :attr:`.ModelAdmin.list_display`
entries that traverse multiple relations using ``__`` could crash or display
incorrect values (:ticket:`37270`).
2 changes: 2 additions & 0 deletions tests/admin_changelist/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Child(models.Model):
class GrandChild(models.Model):
parent = models.ForeignKey(Child, models.SET_NULL, editable=False, null=True)
name = models.CharField(max_length=30, blank=True)
sibling = models.ForeignKey("self", models.SET_NULL, editable=False, null=True)

def __str__(self):
return self.name
Expand All @@ -46,6 +47,7 @@ class Band(models.Model):
class Musician(models.Model):
name = models.CharField(max_length=30)
age = models.IntegerField(null=True, blank=True)
genre = models.ForeignKey(Genre, null=True, on_delete=models.SET_NULL)

def __str__(self):
return self.name
Expand Down
47 changes: 47 additions & 0 deletions tests/admin_changelist/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,53 @@ class ChildAdmin(admin.ModelAdmin):
cl = m.get_changelist_instance(request)
self.assertEqual(cl.get_ordering_field_columns(), {2: "asc"})

def test_list_display_first_degree_relation(self):
parent = Parent.objects.create(name="I am your parent")
child = Child.objects.create(name="I am your child", parent=parent)
grand = GrandChild.objects.create(name="I am your grandchild", parent=child)
GrandChild.objects.create(name="has sibling", parent=child, sibling=grand)

class GrandChildAdmin(admin.ModelAdmin):
list_display = ["name", "sibling"]

m = GrandChildAdmin(GrandChild, custom_site)
request = self._mocked_authenticated_request("/grandchild/", self.superuser)
response = m.changelist_view(request)
self.assertContains(response, '<h2 class="main">I am your grandchild')

def test_list_display_second_degree_relation(self):
parent = Parent.objects.create(name="I am your parent")
child = Child.objects.create(name="I am your child", parent=parent)
GrandChild.objects.create(name="I am your grandchild", parent=child)

class GrandChildAdmin(admin.ModelAdmin):
list_display = ["name", "parent__parent"]

m = GrandChildAdmin(GrandChild, custom_site)
request = self._mocked_authenticated_request("/grandchild/", self.superuser)
response = m.changelist_view(request)
self.assertNotContains(response, "Child object")
self.assertContains(response, "Parent object")

def test_list_display_related_field_uses_display_for_field(self):
# Related fields must be rendered with display_for_field(), not
# display_for_value(), so field-specific formatting, such as FileField
# links, is preserved.
genre = Genre.objects.create(name="Rock", file="documents/test.txt")
Musician.objects.create(name="John", genre=genre)

class MusicianAdmin(admin.ModelAdmin):
list_display = ["name", "genre__file"]

m = MusicianAdmin(Musician, custom_site)
request = self._mocked_authenticated_request("/musician/", self.superuser)
response = m.changelist_view(request)
self.assertContains(
response,
'<a href="/documents/test.txt">documents/test.txt</a>',
html=True,
)

def test_list_display_related_field_boolean_display(self):
"""
Related boolean fields (parent__is_active) display boolean icons.
Expand Down
Loading