Opened 3 hours ago

Last modified 3 hours ago

#36171 assigned Bug

When saving an object(null=True, blank=True) with an empty string __str__ in the admin page.

Reported by: Antoliny Owned by: Antoliny
Component: contrib.admin Version: 5.1
Severity: Normal Keywords: blank, str
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Antoliny)

When a field that allows blank and null is used as __str__, an issue occurs when saving the object in the admin page if the value is empty.

TestCase

class Comment(models.Model):
    author = models.CharField(max_length=128, null=True, blank=True)
    content = models.CharField(max_length=128)

    def __str__(self):
        return self.author


When clicking the save-related button, the following error is returned.

Everytime an object is saved in the admin page, a LogEntry is also created. It seems that an issue occurs when LogEntry tries to use the object's __str__.

My guess is that the to_python method in the CharField(Form) performs a strip() process, causing the value entered in the admin page to be " " instead of "". As a result, it is treated as None, and an issue occurs when LogEntry tries to use __str__ on that value.

Attachments (3)

save_error.png (153.6 KB ) - added by Antoliny 3 hours ago.
error.png (42.8 KB ) - added by Antoliny 3 hours ago.
save_error_resize.png (98.1 KB ) - added by Antoliny 3 hours ago.

Download all attachments as: .zip

Change History (6)

by Antoliny, 3 hours ago

Attachment: save_error.png added

by Antoliny, 3 hours ago

Attachment: error.png added

comment:1 by Antoliny, 3 hours ago

Owner: set to Antoliny
Status: newassigned

comment:2 by Antoliny, 3 hours ago

Description: modified (diff)

by Antoliny, 3 hours ago

Attachment: save_error_resize.png added

comment:3 by Antoliny, 3 hours ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top