Opened 5 weeks ago

Closed 4 weeks ago

Last modified 4 weeks ago

#36114 closed Bug (fixed)

Fields in list_display_links are not links if their values are whitespace.

Reported by: Antoliny Owned by: Antoliny
Component: contrib.admin Version: 5.1
Severity: Normal Keywords: list_display_links, list_display
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Antoliny)

In the admin list page, when the value of list_display is set to 'str' (default option), if the model's __str__ is composed entirely of blank values, the link will not appear.

class Post(models.Model):
    title = models.CharField(max_length=128)
    content = models.TextField()
    like = models.IntegerField(default=0)
    
    def __str__(self):
        return "       "


In a typical situation it would be fine, but when __str__ is set in list_display_links, <a> tag innerHTML becomes empty, so resulting in the loss of the link functionality to navigate to the object.

class PostAdmin(admin.ModelAdmin):
    list_display_links = ["__str__"]
    list_display = ["__str__", "title"]


Attachments (2)

str_blank.png (42.7 KB ) - added by Antoliny 5 weeks ago.
balnk.png (44.4 KB ) - added by Antoliny 5 weeks ago.

Download all attachments as: .zip

Change History (15)

by Antoliny, 5 weeks ago

Attachment: str_blank.png added

by Antoliny, 5 weeks ago

Attachment: balnk.png added

comment:1 by Antoliny, 5 weeks ago

Owner: set to Antoliny
Status: newassigned

comment:2 by Antoliny, 5 weeks ago

Description: modified (diff)

comment:3 by Tim Graham, 5 weeks ago

What's the use case for str being a blank value and what is your proposal to fix it?

in reply to:  3 comment:4 by Antoliny, 5 weeks ago

Replying to Tim Graham:

What's the use case for str being a blank value and what is your proposal to fix it?

I think that a __str__ value being composed entirely of blank spaces is extremely rare unless it's a user mistake. However, for fields, when they consist only of blank spaces, they are treated as None and return the default empty_value("-"). Similarly, when __str__ is composed of only blank spaces, it should return a default empty_value like "-" so that it can function as a link when set in list_display_links.
It seems that adding a bit of logic to the item_for_result function would suffice.

  • django/contrib/admin/templatetags/admin_list.py

    diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
    index 7a49587172..76dfda9d9e 100644
    a b def items_for_result(cl, result, form):  
    226226            if f is None or f.auto_created:
    227227                if field_name == "action_checkbox":
    228228                    row_classes = ["action-checkbox"]
     229                elif field_name == "__str__":
     230                    value = value.strip()
    229231                boolean = getattr(attr, "boolean", False)
    230232                # Set boolean for attr that is a property, if defined.
    231233                if isinstance(attr, property) and hasattr(attr, "fget"):
Last edited 5 weeks ago by Antoliny (previous) (diff)

comment:5 by Tim Graham, 5 weeks ago

Triage Stage: UnreviewedAccepted

comment:6 by Antoliny, 5 weeks ago

Has patch: set

comment:7 by Antoliny, 5 weeks ago

comment:8 by Sarah Boyce, 5 weeks ago

Patch needs improvement: set

comment:9 by Antoliny, 5 weeks ago

Patch needs improvement: unset

comment:10 by Antoliny, 5 weeks ago

Summary: when an object's __str__ consists only of whitespace, the link does not appear in admin list page.Fields in list_display_links are not links if their values are whitespace.

comment:11 by Sarah Boyce, 4 weeks ago

Triage Stage: AcceptedReady for checkin

comment:12 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

Resolution: fixed
Status: assignedclosed

In e262d535:

Fixed #36114 -- Fixed link visibility when list_display_links field contains only whitespace.

comment:13 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

In 5242220:

[5.2.x] Fixed #36114 -- Fixed link visibility when list_display_links field contains only whitespace.

Backport of e262d5355d82901f81fba6c7015643c2b87125bf from main.

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