Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django core: <django.utils.functional.__proxy__ object at ....> is not JSON serializable

I'm using Django 1.8 and in some of my code I just do:

self.request.session['message'] = [
    _(u'Tag!'),
    _(u'Abt!'),
    _(u'Click here to hide this message')]

Then when the page refreshed I have this problem:

<django.utils.functional.__proxy__ object at 0x04805F70> is not JSON serializable

Of course I've googled for it, and I've read the documentation which says that "JSON supports only string keys" and "the JSON serializer from django.core.signing Can only serialize basic data types".

Unless I'm wrong, arrays made of strings are basic data types. Moreover that code has been there for 6 months without a problem.

What am I missing?

like image 656
Olivier Pons Avatar asked Oct 26 '25 07:10

Olivier Pons


1 Answers

It seems that what you are trying to serialise are not strings - they are lazy translation objects (i.e. strings marked for translation, that has not been evaluated yet).

Most likely there is a line in the same file similar to this one:

from django.utils.translation import ugettext_lazy as _

to use a translation function that is not lazy (i.e. it returns translated strings and not lazy translation objects) you should change it to:

from django.utils.translation import ugettext as _

Alternatively you can force evaluation of lazy translation objects before serialising them by calling str() on them.

like image 128
Ludwik Trammer Avatar answered Oct 27 '25 19:10

Ludwik Trammer