Error: XXX is not JSON serializable

805 views
Skip to first unread message

Lee Hinde

unread,
Nov 19, 2013, 12:28:11 PM11/19/13
to django...@googlegroups.com
I have an app I'm moving from local dev (mac, python 2.7.2, django 1.5.4) to WebFaction (python 2.7.5.

I'm storing record ids like so:

section_ids = list(sections.values_list('id', flat=True))
request.session['query.section_ids.last'] = section_ids


works fine locally and fails on WF with:

(as an example)


[10892L, 10891L] is not JSON serializable

This is the traceback...

File "/home/leedhinde/webapps/myapp/lib/python2.7/django/core/handlers/base.py" in get_response
  187.                 response = middleware_method(request, response)
File "/home/leedhinde/webapps/myapp/lib/python2.7/django/contrib/sessions/middleware.py" in process_response
  38.                     request.session.save()
File "/home/leedhinde/webapps/myapp/lib/python2.7/django/contrib/sessions/backends/db.py" in save
  51.             session_data=self.encode(self._get_session(no_load=must_create)),
File "/home/leedhinde/webapps/myapp/lib/python2.7/django/contrib/sessions/backends/base.py" in encode
  84.         serialized = self.serializer().dumps(session_dict)
File "/home/leedhinde/webapps/myapp/lib/python2.7/django/core/signing.py" in dumps
  100.         return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "/usr/local/lib/python2.7/json/__init__.py" in dumps
  250.         sort_keys=sort_keys, **kw).encode(obj)
File "/usr/local/lib/python2.7/json/encoder.py" in encode
  207.         chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/lib/python2.7/json/encoder.py" in iterencode
  270.         return _iterencode(o, 0)
File "/usr/local/lib/python2.7/json/encoder.py" in default
  184.         raise TypeError(repr(o) + " is not JSON serializable")


I'm not clear why this wouldn't work. I also haven't figured out testing session updating in the shell for simpler testing.

Any pointers would be appreciated.

tim

unread,
Nov 19, 2013, 3:58:47 PM11/19/13
to django...@googlegroups.com
Are you sure have the list() cast in the deployed version of the code? Trying to serialize a ValuesListQuerySet directly would give the error you mention since repr() of a ValuesListQuerySet looks like a list.

Lee Hinde

unread,
Nov 19, 2013, 4:10:33 PM11/19/13
to django...@googlegroups.com
Thanks; I'm almost positive. I'll double check. I've worked around the problem by converting first.
Reply all
Reply to author
Forward
0 new messages