JSONField gets save as django string

I have a django model as shown below:

from jsonfield import JSONField
class SCUser(User):
    address = JSONField(blank=True,null=True)

When I save json in this address, it is saved as a string. Here is the code snippet:

appuser.address = {"state":""}
appuser.save()

Now, if I try to restore appuser.address, it gives me

>>>appuser.address
>>>u'{"state":""}'
>>>appuser.save()
>>>appuser.address
>>>u'"{\\"state\\":\\"\\"}"'

And it becomes recursive. What am I missing here?

Edit: AppUser inherits from the SCUser model.

+5
source share
2 answers

I ran into this problem when I used a key other than Autofield as the model’s primary key and found some problems that are still open on github related to this problem.

https://github.com/dmkoch/django-jsonfield/issues/92

https://github.com/dmkoch/django-jsonfield/issues/101

, pk . , .

class SCUser(User):
    ....

    @property
    def pk(self):
        return self.id  # your pk
0

:

appuser.address = {"state":""}
appuser.save()
appuser.get_data_json()
-1

Source: https://habr.com/ru/post/1611257/


All Articles