[Django] #36171: When saving an object(null=True, blank=True) with an empty string __str__ in the admin page.

11 views
Skip to first unread message

Django

unread,
Feb 4, 2025, 10:42:58 PMFeb 4
to django-...@googlegroups.com
#36171: When saving an object(null=True, blank=True) with an empty string __str__
in the admin page.
----------------------------+-----------------------------------------
Reporter: Antoliny | Type: Bug
Status: new | Component: contrib.admin
Version: 5.1 | Severity: Normal
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
----------------------------+-----------------------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/36171>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 4, 2025, 10:43:07 PMFeb 4
to django-...@googlegroups.com
#36171: When saving an object(null=True, blank=True) with an empty string __str__
in the admin page.
-------------------------------+--------------------------------------
Reporter: Antoliny | Owner: (none)
Type: Bug | Status: new
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
-------------------------------+--------------------------------------
Changes (by Antoliny):

* Attachment "save_error.png" added.

Django

unread,
Feb 4, 2025, 10:43:10 PMFeb 4
to django-...@googlegroups.com
#36171: When saving an object(null=True, blank=True) with an empty string __str__
in the admin page.
-------------------------------+--------------------------------------
Reporter: Antoliny | Owner: (none)
Type: Bug | Status: new
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
-------------------------------+--------------------------------------
Changes (by Antoliny):

* Attachment "error.png" added.

Django

unread,
Feb 4, 2025, 10:43:22 PMFeb 4
to django-...@googlegroups.com
#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
-------------------------------+--------------------------------------
Changes (by Antoliny):

* owner: (none) => Antoliny
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36171#comment:1>

Django

unread,
Feb 4, 2025, 10:44:48 PMFeb 4
to django-...@googlegroups.com
#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

unread,
Feb 4, 2025, 10:44:55 PMFeb 4
to django-...@googlegroups.com
#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
-------------------------------+--------------------------------------
Changes (by Antoliny):

* Attachment "save_error_resize.png" added.

Django

unread,
Feb 4, 2025, 10:47:11 PMFeb 4
to django-...@googlegroups.com
#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
> }}}
Ticket URL: <https://code.djangoproject.com/ticket/36171#comment:3>

Django

unread,
Feb 5, 2025, 6:10:57 AMFeb 5
to django-...@googlegroups.com
#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: closed
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution: invalid
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
-------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => invalid
* status: assigned => closed

Comment:

To me, the issue here is in the string method should always return a
string (eg. `return self.author or ""`)
If you want to keep spaces, you will need a custom form with `strip=False`
(see https://docs.djangoproject.com/en/5.1/ref/forms/fields/#charfield)
--
Ticket URL: <https://code.djangoproject.com/ticket/36171#comment:4>

Django

unread,
Feb 5, 2025, 6:20:49 AMFeb 5
to django-...@googlegroups.com
#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: closed
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution: invalid
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
-------------------------------+--------------------------------------
Comment (by Antoliny):

sarahboyce, I have a small question. 🙋
It seems this issue occurs because even though we set `blank=True` in the
`Field(Model)`, but the admin `Form` doesn't allow empty value.
Is this how it was originally designed? Does this mean users need to
manually configure `Form` to allow empty fields for `Field(Model)` where
`blank=True` is set?
--
Ticket URL: <https://code.djangoproject.com/ticket/36171#comment:5>
Reply all
Reply to author
Forward
0 new messages