#26391: JSONField empty_values
-------------------------------------+-------------------------------------
     Reporter:  sim1234              |                    Owner:  (none)
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  contrib.postgres     |                  Version:  1.9
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:  JSON JSONField       |             Triage Stage:
  postgres forms                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  1                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Matt Hegarty):

 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 [https://stackoverflow.com/a/56271722/39296 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})
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/26391#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.fedd0519caec8de5432660d3f272ad5d%40djangoproject.com.

Reply via email to