Here is how to reproduce the bug with shell:
Let's say there is a model named MyModel with a JSONField defined as
follows:
{{{
#!python
json_field = JSONField(
blank=True,
null=True,
default=dict
)
}}}
Now, after importing neccesary models, let's do some data IO operations in
shell:
{{{
#!python
my_model = MyModel.objects.get(id=1)
my_model.json_field = {"foo":"bar"}
my_model.save()
my_model.json_field
}}}
The last command prints out: {'foo': 'bar'} which is perfectly fine.
Let's now fetch the model again and print the value of the json field:
{{{
#!python
my_model = MyModel.objects.get(id=1)
my_model.json_field
}}}
This prints out '{"foo": "bar"}', so we se that the dict has been
converted to string (probably somewhere with json.dumps). If we run
my_model.save() without changing the value of json_field (which is a real-
world scenario, for instance we might have wanted to change other fields
and then run save) and fetch it again, the value of json_field wil be
doubly-encoded, although the value already is a valid JSON string :
{{{
#!python
my_model.save()
my_model = MyModel.objects.get(id=1)
my_model.json_field
}}}
Last command now prints out {{{ '"{\\"foo\\": \\"bar\\"}"' }}}. Obviously
the string has now been re-encoded, causing some characters to be escaped.
Next iteration of these steps results in:
{{{ '"\\"{\\\\\\"foo\\\\\\": \\\\\\"bar\\\\\\"}\\""' }}}
If we repeat these actions multiplte times the value will grow in a very
fast manner due to escaping the backslashes with backslashes. In only a
few iterations I managed to have pg_dump (only for data) create a 1GB
output file.
I am not sure whether the the cause for this bug resides in Django's
implementation of the JSONField or maybe in the psycopgb's implementation
of postgres JSONField, but somewhere on the fetch from database, the value
of the field is being encoded to string with json.dumps, and this is being
repeatedly done on every fetch, despite the value being already encoded to
a valid JSON string. It is my assumption that this happens on fetch, it
might be the case, that on update (my_model.save()) re-encoding and thus
re-escaping takes place.
Same happens if we never change the value of the json_field. If it
initially was an empty json obj {} , after only a few iterations it will
grow to {{{ '"\\"\\\\\\"{}\\\\\\"\\""' }}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27538>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* type: Uncategorized => Bug
* resolution: => worksforme
* severity: Release blocker => Normal
Comment:
I wrote the attached test which passes for me. Could you please provide a
test that fails for you?
--
Ticket URL: <https://code.djangoproject.com/ticket/27538#comment:1>
* Attachment "27538-test.diff" added.
Comment (by Petar Aleksic):
I didn't manage to reproduce the bug with the test or in a fresh django
project.
--
Ticket URL: <https://code.djangoproject.com/ticket/27538#comment:2>
* status: closed => new
* resolution: worksforme =>
Comment:
I have the same problem, Re-json the JSONField value.
{{{
from django.contrib.postgres.fields import JSONField
class Foo(models.Model):
photos = JSONFields(max_length=300)
>>f = Foo()
>>f.photos = []
>>f.save()
>>f.photos
u'[]'
>>f.save()
>>f.refresh_from_db()
>>f.photos
u'"[]"'
>>f.save()
>>f.refresh_from_db()
>>f.photos
u'"\\"[]\\""'
}}}
Above was in manage.py shell, and everytime I save the instance, the
JSONField value is re-jsonized.
env:
postgres 9.6.1
python 2.7.12
django 1.10.5
psycopg2 2.6.2
--
Ticket URL: <https://code.djangoproject.com/ticket/27538#comment:3>
* status: new => closed
* resolution: => duplicate
Comment:
This behavior doesn't reproduce in Django's test suite. Are you using
`django-jsonfield`? See ticket:27675#comment:8.
--
Ticket URL: <https://code.djangoproject.com/ticket/27538#comment:4>