{{{#!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.
* 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>
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>