I have a ListView where I show a list of objects.
The object has got a JSONField and the json string is automatically converted with json with single quotes.
However, I need it to stay with double quotes to prettify it on template as I need to render the field on django template using html and javascript.
I am not sure how to prevent it to be converted to single quotes.
Thanks in advance.
This is not a Django JSONField issue, this is a standard Python / dict representation issue.
From an iPython shell:
In [18]: {"a": "This is a test"}
Out[18]: {'a': 'This is a test'}
Or from a regular Python shell:
>>> {"a": "This is a test"}
{'a': 'This is a test'}
>>>
I’m guessing that your only real choice here would be to create your own __repr__
method for that field and use it to generate the string representation of the dict however you see fit.
I think the the dumps
method in the Python json
module is what you’re looking for here.
From an ipython shell:
In [4]: d=[{'a':1, 'b': 'Another test'}, [1,2,3], "abc"]
In [5]: d
Out[5]: [{'a': 1, 'b': 'Another test'}, [1, 2, 3], 'abc']
In [6]: json.dumps(d)
Out[6]: '[{"a": 1, "b": "Another test"}, [1, 2, 3], "abc"]'
1 Like