I was trying to serialize a Python list but got errors that it’s not serializable. Is there a limitation on serializing a list of Long integers?
>>> ids=p.values_list('id',flat=True)
>>> ids
[335L, 468L, 481L, 542L, 559L, 567L, 609L]
>>> import simplejson as json
>>> str=json.dumps(ids)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Program Files\Google\google_appengine\lib\simplejson\simplejson\__ini
t__.py", line 265, in dumps
return _default_encoder.encode(obj)
File "C:\Program Files\Google\google_appengine\lib\simplejson\simplejson\encod
er.py", line 216, in encode
chunks = list(chunks)
File "C:\Program Files\Google\google_appengine\lib\simplejson\simplejson\encod
er.py", line 495, in _iterencode
o = _default(o)
File "C:\Program Files\Google\google_appengine\lib\simplejson\simplejson\encod
er.py", line 190, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: [335L, 468L, 481L, 542L, 559L, 567L, 609L] is not JSON serializable
>>>
“I am trying to serialize a Python list…”
This is actually not quite the full story.
You are trying to serialize a
ValuesListQuerySet.You can either
1. convert to a Python list as mentioned in the other great answers, or
2. serialize just the IDs.
Django has a built-in way to serialize a
QuerySet.And you only want the IDs so you may use the
fieldskwarg.