4

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?

Dhia
  • 10,119
  • 11
  • 58
  • 69
pzijd
  • 781
  • 1
  • 6
  • 9
  • maybe it's a problem with collation in your database? – middlestump Aug 09 '16 at 12:27
  • SHOW LC_COLLATE gives "C". Do you think it has to be set to "UTF8"? – pzijd Aug 10 '16 at 07:17
  • I think that's a good idea. If you can't change for the whole database now, you an change only for this column. – middlestump Aug 10 '16 at 07:57
  • Just saw this answer: http://stackoverflow.com/a/38867411/5357503 may help – middlestump Aug 10 '16 at 08:03
  • The collation is now set to "en_US.UTF-8" but I still do need to replace the string. – pzijd Aug 11 '16 at 09:29
  • The example code calls `serialize`, but there is no indication where this comes from. Can you instead re-write the example to be a [complete, minimal, verifiable example](https://stackoverflow.com/help/mcve) that we can try running? – bignose Apr 16 '18 at 01:48

0 Answers0