Environment:
Example model:
from django.db import models
from django.contrib.postgres.fields import JSONField
class Foo(models.Model):
data = JSONField()
When I try to create an object, everything works as expected:
from myapp.models import Foo
x = Foo()
x.data = {'key1': 'value1'}
x.save()
And querying works as expected:
Foo.objects.filter(data__key1='value1').count()
# 1
However, when I try to retrieve that data from the object, the value of the .data
attribute is a string:
from myapp.models import Foo
x = Foo.objects.get(id=1)
x.data
# '{"key1": "value1"}'
type(x.data)
# str
I would expect to get back a dict here. The problem gets recursively worse when trying to save back the object
x.save()
x = Foo.objects.get(id=1)
x.data
# '"{\\"key1\\": \\"value1\\"}"'
x.save()
x = Foo.objects.get(id=1)
x.data
# '"\\"{\\\\\\"key1\\\\\\": \\\\\\"value1\\\\\\"}\\""'
Turns out you cannot use django-jsonfield and Django's native JSONField in the same project or will you run into the weird behavior as described in the question
https://bitbucket.org/schinckel/django-jsonfield/issues/57/cannot-use-in-the-same-project-as-djangos
For me the problem was that the database was created from a backup and the column type was set to text
when it should have been either json
or jsonb
.
Cleaning any invalid json then altering the column type with the following:
ALTER TABLE t ALTER COLUMN j TYPE jsonb USING j::text::jsonb;
(thanks to https://stackoverflow.com/a/28076909/2362877)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With