Django provides us with ArrayField: a PostgreSQL specific model field which allows storing lists of data.
I use it to model an Article object:
authors = ArrayField(models.Charfield)
title = models.CharField
For example, here is an entry in the database:
- authors = {"Pierre Stévens", "Jacques Dupont"}
- title = Sur le terrain crétacé de Liège
I would like to export all the articles as a JSON:
query_set = Article.objects.all()
articles_json = serialize('json', fields('authors', 'title')
The serialization of the ArrayField "authors" escapes the accented character "é" with four backslashes instead of two. The regular "title" field is correctly escaped. Look at the difference:
articles_json = {"title":"Sur le terrain cr\\u00e9tac\\u00e9 de Li\\u00e9ge", "authors": "[\\"Pierre St\\\\u00e9\\", "Jacques Dupont"]"}
This is not understood by the client. To make it works, I just searched and replaced the string
articles_json = articles_json.replace('\\\\','\\')
I'm really not sure of this workaround. Did you experiment this problem? How did or would you solve it?