#36171: When saving an object(null=True, blank=True) with an empty string
__str__
in the admin page.
-------------------------------+--------------------------------------
Reporter: Antoliny | Owner: Antoliny
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution:
Keywords: blank, str | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Antoliny:
Old 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
> }}}
> [[Image(save_error.png)]]
> When clicking the save-related button, the following error is returned.
> [[Image(error.png)]]
> 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.
New 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
}}}
[[Image(save_error_resize.png)]]
When clicking the save-related button, the following error is returned.
[[Image(error.png)]]
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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/36171#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/01070194d435cf07-1aeb6903-f594-4f39-a22b-6271ffe2136a-000000%40eu-central-1.amazonses.com.