Opened 9 years ago

Closed 9 years ago

Last modified 4 years ago

#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)

invalid_empty_json_field.png (4.9 KB ) - added by Matt Hegarty 4 years ago.

Download all attachments as: .zip

Change History (7)

comment:2 by Tim Graham, 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 Tim Graham, 9 years ago

Resolution: needsinfo
Status: newclosed

comment:4 by Matt Hegarty, 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 Matt Hegarty, 4 years ago

comment:5 by Tim Graham, 4 years ago

That's discussed in #27697.

comment:6 by Matt Hegarty, 4 years ago

ok - thanks Tim

Note: See TracTickets for help on using tickets.
Back to Top