[Django] #27697: JSONField with blank=True is rendered as non-required field in a ModelForm

265 views
Skip to first unread message

Django

unread,
Jan 6, 2017, 8:51:35 AM1/6/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
--------------------------------------------+--------------------------
Reporter: aruseni | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 1.10
Severity: Normal | Keywords: postgres
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+--------------------------
Try adding a JSONField with blank=True and without null=True to your
model, and then use this model in the admin, the field will be rendered as
a non-required field, and if you specify {} as the value, everything saves
just fine.

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.

Django

unread,
Jan 9, 2017, 7:03:39 AM1/9/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
----------------------------------+--------------------------------------

Reporter: aruseni | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 1.10
Severity: Normal | Resolution:

Keywords: postgres | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

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>

Django

unread,
Jan 20, 2017, 7:25:23 PM1/20/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
----------------------------------+------------------------------------

Reporter: aruseni | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 1.10
Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

Accepting as the current behavior doesn't seem ideal.

--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:2>

Django

unread,
Jan 30, 2017, 6:44:52 AM1/30/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.10

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Andrew Nester):

* 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>

Django

unread,
Jan 30, 2017, 6:45:09 AM1/30/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.10

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Andrew Nester):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:4>

Django

unread,
Feb 6, 2017, 4:15:16 AM2/6/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.10

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Feb 6, 2017, 4:22:02 AM2/6/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.10

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Andrew Nester):

Thanks Sayid! Just fixed it in my PR

--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:6>

Django

unread,
Feb 9, 2017, 7:40:23 PM2/9/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.10

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* 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>

Django

unread,
Feb 10, 2017, 6:29:13 AM2/10/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.10

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Andrew Nester):

* Attachment "Screen Shot 2017-02-10 at 2.19.21 PM.png" added.

Form field error

Django

unread,
Feb 10, 2017, 6:30:25 AM2/10/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.10

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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.}}}

[[Image(https://code.djangoproject.com/attachment/ticket/27697/Screen%20Shot%202017-02-10%20at%202.19.21%20PM.png)]]

--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:8>

Django

unread,
Feb 10, 2017, 6:32:03 AM2/10/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.10

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Andrew Nester):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:9>

Django

unread,
Feb 16, 2017, 7:17:49 PM2/16/17
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 1.10

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* 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>

Django

unread,
Jan 15, 2021, 5:40:12 AM1/15/21
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |

Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* component: contrib.postgres => Database layer (models, ORM)


--
Ticket URL: <https://code.djangoproject.com/ticket/27697#comment:11>

Django

unread,
Jul 19, 2021, 1:25:10 PM7/19/21
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jul 27, 2021, 8:53:35 AM7/27/21
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Andrew
| Nester
Type: Bug | Status: closed

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: postgres | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* 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>

Django

unread,
Jul 27, 2021, 9:48:46 AM7/27/21
to django-...@googlegroups.com
#27697: JSONField with blank=True is rendered as non-required field in a ModelForm
-------------------------------+-----------------------------------------

Reporter: aruseni | Owner: Andrew Nester
Type: Bug | Status: closed
Component: Documentation | Version: 1.10

Severity: Normal | Resolution: duplicate
Keywords: postgres | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Changes (by Jacob Walls):

* 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>

Reply all
Reply to author
Forward
0 new messages