[Django] #35259: forms.JSONField validator doesn't match documentation behavior

10 views
Skip to first unread message

Django

unread,
Feb 28, 2024, 6:34:33 AM2/28/24
to django-...@googlegroups.com
#35259: forms.JSONField validator doesn't match documentation behavior
-----------------------------------------+---------------------------
Reporter: jklingen92 | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Keywords: JSONField
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+---------------------------
If you have a required JSONField on a model and you make a model form, the
model form rejects falsey values as empty ([] for sure, possibly others as
well). The documentation says that None is the empty value for the
forms.JSONField.

I've seen similar documented issues with regards to the admin panel, not
sure if they're running off the same code or not, but it doesn't seem to
be resolved in this case.

For example:

{{{
class FooForm(forms.Form)
bar = forms.JSONField(required=True)

# in shell
f = FooForm(data={"bar": []})
f.is_valid() # False
f.errors # {'bar': ['This field is required.']}
}}}

This contradicts the documentation, as well as the behavior of the JSON
model field, so that JSON fields create undesirable behavior in
ModelForms.

I'm using Django 3.2, not sure if this behavior is fixed in a later
version.
--
Ticket URL: <https://code.djangoproject.com/ticket/35259>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 28, 2024, 7:14:42 AM2/28/24
to django-...@googlegroups.com
#35259: forms.JSONField validator doesn't match documentation behavior
-----------------------------------+--------------------------------------
Reporter: Jake Klingensmith | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 5.0
Severity: Normal | Resolution: duplicate
Keywords: JSONField | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* component: Uncategorized => Forms
* easy: 1 => 0
* resolution: => duplicate
* status: new => closed

Comment:

Duplicate of #26391.
--
Ticket URL: <https://code.djangoproject.com/ticket/35259#comment:1>

Django

unread,
Feb 28, 2024, 7:16:36 AM2/28/24
to django-...@googlegroups.com
#35259: forms.JSONField validator doesn't match documentation behavior
-----------------------------------+--------------------------------------
Reporter: Jake Klingensmith | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 5.0
Severity: Normal | Resolution: duplicate
Keywords: JSONField | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Comment (by David Sanders):

Also just FYI the documentation is actually referring to what JSONField
normalises the empty value to:
https://github.com/django/django/blob/main/django/forms/fields.py#L1372
The documentation clarifies this normalisation:
https://docs.djangoproject.com/en/5.0/ref/forms/fields/#django.forms.Field.required
--
Ticket URL: <https://code.djangoproject.com/ticket/35259#comment:2>
Reply all
Reply to author
Forward
0 new messages