Opened 9 months ago
Last modified 9 months ago
#36171 closed Bug
When saving an object(null=True, blank=True) with an empty string __str__ in the admin page. — at Initial Version
| Reported by: | Antoliny | Owned by: | |
|---|---|---|---|
| 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
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.