Using Django JSON serializer for object that is no

2019-07-10 02:34发布

  • 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 ... __init__.py:385] 'str' object has no attribute '_meta'

2条回答
▲ chillily
2楼-- · 2019-07-10 03:01

The GQLEncoder class in this library can take a db.Model entity and serialize it. I'm not sure if this is what you're looking for, but it's been useful to me.

查看更多
虎瘦雄心在
3楼-- · 2019-07-10 03:15

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.

查看更多
登录 后发表回答