Django ArrayField serialization escapes accented characters with too much backslash

Django provides us with ArrayField: a field of a specific PostgreSQL model that allows you to store 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 articles as JSON :

 query_set = Article.objects.all() articles_json = serialize('json', fields('authors', 'title') 

The serialization of the "authors" ArrayField " eludes the accented" é "character with four backslashes instead of two . The correct header 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 work, I just searched and replaced the string

 articles_json = articles_json.replace('\\\\','\\') 

I'm really not sure about this workaround. Have you experimented with this problem? How was it or will you decide?

+5
source share

All Articles