django simplejson [] is not JSON serializable -


in view, why work:

results = [] results.append({'status':1}) results.append({'bookmarks':[]}) simplejson.dumps(results) # produces: [] 

and doesn't:

from myapp.models import bookmark results = [] results.append({'status':1}) results.append({'bookmarks':bookmark.objects.all()}) # fails exception saying: [] not json serializable 

completely stack trace follows

traceback: file "/users/ishaq/projects/github/bookmarks/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response   115.                         response = callback(request, *callback_args, **callback_kwargs) file "/users/ishaq/projects/github/bookmarks/bookmarks/views.py" in index   9.   return httpresponse(simplejson.dumps(bookmark.objects.all()), mimetype='application/json'); file "/usr/local/cellar/python/2.7.3/frameworks/python.framework/versions/2.7/lib/python2.7/json/__init__.py" in dumps   231.         return _default_encoder.encode(obj) file "/usr/local/cellar/python/2.7.3/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py" in encode   201.         chunks = self.iterencode(o, _one_shot=true) file "/usr/local/cellar/python/2.7.3/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py" in iterencode   264.         return _iterencode(o, 0) file "/usr/local/cellar/python/2.7.3/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py" in default   178.         raise typeerror(repr(o) + " not json serializable")  exception type: typeerror @ /conferences/ exception value: [] not json serializable 

instead of using simplejson serialize django objects, use serialization provided django.

with reference form link, can do:

from django.core import serializers data = serializers.serialize("json", bookmark.objects.all()) 

Comments