Perşembe, Haziran 14, 2007

JsonResponse

A subclass of HttpResponse useful as a shortcut in views; it chooses the correct JSON serializer based on whether or not it is passed a QuerySet.
from django.core.serializers import serialize
from django.db.models.query import QuerySet
from django.http import HttpResponse
from django.utils import simplejson

class JsonResponse(HttpResponse):
def __init__(self, object):
if isinstance(object, QuerySet):
content = serialize('json', object)
else:
content = simplejson.dumps(object)
super(JsonResponse, self).__init__(content, mimetype='application/json')

Hiç yorum yok: