#26391 closed Cleanup/optimization (needsinfo)
JSONField empty_values
Reported by: | sim1234 | Owned by: | |
---|---|---|---|
Component: | contrib.postgres | Version: | 1.9 |
Severity: | Normal | Keywords: | JSON JSONField postgres forms |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
When validating required JSONField with values '{}', '[]' or '""' a ValidationError is raised.
In case of JSON, only None (null) should be considered an empty value.
Attachments (1)
Change History (7)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
Regression tests are needed to demonstrate what this fixes. Also the current patch causes a regression of postgres_tests.test_json.TestFormField.test_valid_empty
.
comment:3 by , 9 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:4 by , 4 years ago
Can we re-open this as a bug? The default behaviour of JSONFields in the admin console is to fail when you try to save (see attached).
Also raised on SO
The workaround is:
# forms.py class ValidEmptyJSONField(forms.JSONField): empty_values = [None, "", [], ()] from django.db.models import JSONField class ValidEmptyJSONField(JSONField): """ Overridden to permit valid empty JSON. """ empty_values = [None, "", [], ()] def formfield(self, **kwargs): from app import forms return super().formfield(**{"form_class": forms.ValidEmptyJSONField, **kwargs})
by , 4 years ago
Attachment: | invalid_empty_json_field.png added |
---|
Note:
See TracTickets
for help on using tickets.
Pull request: https://github.com/django/django/pull/6320