#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 )
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)
Change History (15)
by , 5 weeks ago
Attachment: | str_blank.png added |
---|
by , 5 weeks ago
comment:1 by , 5 weeks ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:2 by , 5 weeks ago
Description: | modified (diff) |
---|
follow-up: 4 comment:3 by , 5 weeks ago
comment:4 by , 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): 226 226 if f is None or f.auto_created: 227 227 if field_name == "action_checkbox": 228 228 row_classes = ["action-checkbox"] 229 elif field_name == "__str__": 230 value = value.strip() 229 231 boolean = getattr(attr, "boolean", False) 230 232 # Set boolean for attr that is a property, if defined. 231 233 if isinstance(attr, property) and hasattr(attr, "fget"):
comment:5 by , 5 weeks ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:6 by , 5 weeks ago
Has patch: | set |
---|
comment:8 by , 5 weeks ago
Patch needs improvement: | set |
---|
comment:9 by , 5 weeks ago
Patch needs improvement: | unset |
---|
comment:10 by , 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 , 4 weeks ago
Triage Stage: | Accepted → Ready for checkin |
---|
What's the use case for str being a blank value and what is your proposal to fix it?