[Django] #26110: Admin form escapes postgres JSONField on ValidationError

30 views
Skip to first unread message

Django

unread,
Jan 21, 2016, 3:59:28 AM1/21/16
to django-...@googlegroups.com
#26110: Admin form escapes postgres JSONField on ValidationError
-------------------------------+------------------------------
Reporter: outime | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.9
Severity: Normal | Keywords: jsonfield, admin
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------
To reproduce this bug, let's assume we have this model:

{{{#!python
class TestModel(models.Model):
test_url = models.URLField() # I've chosen URLField as an example but
the specific type of field is irrelevant for this issue
json_field = JSONField(default={})
}}}

The model is added to the admin site the standard way
(`admin.site.register()`) so that we can add content easily. Now, if you
go and submit the following:

`test_url` → `test`
`json_field` → `{"hello": "hola"}`

It'll trigger an expected `ValidationError` since it's not a valid URL.
When the form is rendered and presented back to the user, the `test_url`
will show the incorrect URL while the `json_field` will look like this:
`"{\"hello\": \"hola\"}"`. If the user corrects the `test_url` and submits
the form again, the `json_field` that's being stored in the DB will be the
escaped one.

I can't say if this only happens with admin forms or any form (e.g. model
forms) since I have no time to run proper tests, but I hope this is enough
to track down the issue.

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

Django

unread,
Jan 21, 2016, 6:35:01 AM1/21/16
to django-...@googlegroups.com
#26110: Admin form escapes postgres JSONField on ValidationError
----------------------------------+--------------------------------------
Reporter: outime | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 1.9
Severity: Normal | Resolution: duplicate

Keywords: jsonfield, admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => closed
* needs_better_patch: => 0
* resolution: => duplicate
* needs_tests: => 0
* needs_docs: => 0


Comment:

Duplicate of #25532

--
Ticket URL: <https://code.djangoproject.com/ticket/26110#comment:1>

Django

unread,
Jun 4, 2018, 9:38:29 PM6/4/18
to django-...@googlegroups.com
#26110: Admin form escapes postgres JSONField on ValidationError
----------------------------------+--------------------------------------
Reporter: Rubén Díaz | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 1.9
Severity: Normal | Resolution: duplicate

Keywords: jsonfield, admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by Laurynas Riliskis):

I am running Django 2 and encounter the same problem. The same results
are obtained is called though the admin interface.

{{{
>>> p = JSONTest.objects.get(instance="4550d7f3a28c4081af5b3ff5ac62ab60")
>>> p.data
'{}'
>>> p.save()
>>> p = JSONTest.objects.get(instance="4550d7f3a28c4081af5b3ff5ac62ab60")
>>> p.data
'"{}"'
>>> p.save()
>>> p = JSONTest.objects.get(instance="4550d7f3a28c4081af5b3ff5ac62ab60")
>>> p.data
'"\\"{}\\""'
>>> p.save()
>>> p = JSONTest.objects.get(instance="4550d7f3a28c4081af5b3ff5ac62ab60")
>>> p.data
'"\\"\\\\\\"{}\\\\\\"\\""'
}}}

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

Reply all
Reply to author
Forward
0 new messages