Python – Using Django JSON serializer for object that is not a Model


  • Is it possible to use Django serializer without a Model?
  • How it is done?
  • Will it work with google-app-engine?

I don't use Django framework, but since it is available, I would want to use its resources here and there.
Here is the code I tried:

from django.core import serializers
obj = {'a':42,'q':'meaning of life'}
serialised = serializers.serialize('json', obj)

this generates an error

ERROR ...] 'str' object has no attribute '_meta'

Best Solution

Serializers are only for models. Instead you can use simplejson bundled with Django.

from django.utils import simplejson
json_str = simplejson.dumps(my_object)

Simplejson 2.0.9 docs are here.