2009-06-17 66 views
2
  • 是否可以在沒有模型的情況下使用Django序列化程序?
  • 它是如何完成的?
  • 它可以與谷歌應用程序引擎?

我不使用Django框架,但由於它是可用的,我想在這裏和那裏使用它的資源。 這裏是我試過的代碼:對不是模型的對象使用Django JSON序列化程序

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

這會產生一個錯誤

ERROR ... __init__.py:385] 'str' object has no attribute '_meta' 

回答

15

串行器僅用於模型。相反,您可以使用與Django捆綁在一起的simplejson

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

Simplejson 2.0.9 docs是here

+0

+1:你可以在http://simplejson.googlecode.com/svn/tags/simplejson-2.0.9/docs/index.html – 2009-06-17 10:26:16

0

該庫中的GQLEncoder類可以帶一個db.Model實體並對其進行序列化。我不確定這是你想要的,但它對我有用。

相關問題