I\'d like to use simplejson to serialize a Django model. Django\'s serializer doesn\'t support dictionaries... and simplejson doesn\'t support Django Querysets. This is quit
based on Clement's answer, I did this to get models into JSON as well.
def toJSON(obj):
if isinstance(obj, QuerySet):
return simplejson.dumps(obj, cls=DjangoJSONEncoder)
if isinstance(obj, models.Model):
#do the same as above by making it a queryset first
set_obj = [obj]
set_str = simplejson.dumps(simplejson.loads(serialize('json', set_obj)))
#eliminate brackets in the beginning and the end
str_obj = set_str[1:len(set_str)-2]
return str_obj