If you don’t specify any value though, an error occurs:
{{{
null value in column "json" violates not-null constraint
}}}
If you just use a zero-configuration JSONField without blank=True, then
the form will not validate when an empty dictionary is specified.
--
Ticket URL: <https://code.djangoproject.com/ticket/27697>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Tim Graham):
What do you think the expected behavior is in this case?
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:1>
* stage: Unreviewed => Accepted
Comment:
Accepting as the current behavior doesn't seem ideal.
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:2>
* owner: (none) => Andrew Nester
* status: new => assigned
Comment:
I've added pull request [https://github.com/django/django/pull/7990 #PR]
I've decided to make logic so: if we have {{{blank=True}}} and
{{{null=False}}} for {{{JSONField}}}, we make this field required with
empty value set to {{{{} }}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:3>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:4>
Comment (by Sayid Munawar):
CMIIW, in the
[https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/#jsonfield
Django Docs] it is not advised to use `{}` as an empty value. it should be
callable like `dict`
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:5>
Comment (by Andrew Nester):
Thanks Sayid! Just fixed it in my PR
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:6>
* needs_better_patch: 0 => 1
Comment:
The behavior with a field like `data = JSONField(blank=True)` still seems
confusing. It renders "null" in an initial form but then creates a "This
field is required." error if you try to save that. Before this PR, a value
of `{}` could be saved with such a field, but now it gives "This field is
required." for that input. Is this what you expected? If so, perhaps some
documentation clarification is in order as this doesn't seem intuitive.
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:7>
* Attachment "Screen Shot 2017-02-10 at 2.19.21 PM.png" added.
Form field error
Comment (by Andrew Nester):
Thanks Tim!
I've just updated my PR to make it work more clear.
Now it's working like this.
If we have {{{blank=True}}} set , form field will be render as NOT
required and valid empty JSON string such as {}, [] will be successfully
validated and saved.
But if you try to submit just empty form field, you will get form
validation error with message {{{ 'None' value must be valid JSON.}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:8>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:9>
* needs_better_patch: 0 => 1
Comment:
I think what I would expect (and I'm certain open to other opinions,
including your own if you disagree) with `blank=True` is that nothing is
rendered in the widget and if nothing is submitted, that empty string is
transformed into an empty dict so it can be saved. In particular, I'm
thinking of an API context where the `initial={}` that your proposed isn't
going to have an effect since the API is only submitted values and doesn't
receive an initial form.
I wish we could solve the null case without adding a new form option
("invalidate_null" as you proposed). If "null" is submitted though, an
error message might indeed be more appropriate than transforming that into
an empty dict. If we need to add some new form option, perhaps
`allow_null=True` (default) would be more consistent with other existing
options such as `allow_unicode`.
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:10>
* component: contrib.postgres => Database layer (models, ORM)
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:11>
Comment (by Jacob Walls):
I suggest marking as a duplicate of #22224. Any proposal to run model-
validation on not-nullable blankable fields would fix both (but I suggest
`wontfix`ing #22224 as the status quo represents a supported design
pattern, will comment there shortly).
Re: Tim's suggestion:
> if nothing is submitted, that empty string is transformed into an empty
dict so it can be saved
I think this is dangerous because we should preserve the distinction
between `None` and `{}`, in case the field is nullable. There could be
code depending on this distinction.
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:12>
* status: assigned => closed
* resolution: => duplicate
Comment:
Per [https://groups.google.com/g/django-developers/c/GlYM25fdRnA
discussion] we're going to have a look at clarifying the documentation
around the blank=True, null=False idiom, and we can do that in #22224.
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:13>
* needs_better_patch: 1 => 0
* has_patch: 1 => 0
* component: Database layer (models, ORM) => Documentation
--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:14>