#35259 closed Bug (duplicate)
forms.JSONField validator doesn't match documentation behavior
Reported by: | Jake Klingensmith | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 5.0 |
Severity: | Normal | Keywords: | JSONField |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Change History (2)
comment:1 by , 9 months ago
Component: | Uncategorized → Forms |
---|---|
Easy pickings: | unset |
Resolution: | → duplicate |
Status: | new → closed |
comment:2 by , 9 months ago
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
Duplicate of #26391.