from django.core.serializers.json import DjangoJSONEncoder import simplejson as json from django.db.models.query import QuerySet from django.http import HttpResponse from django.core import serializers class JsonResponse(HttpResponse): error = "" __data = [] def __set_data(self, data): self.__data = (isinstance(data, QuerySet) or hasattr(data[0], '_meta'))\ and serializers.serialize('python', data) or data data = property(fset = __set_data) def __get_container(self): return json.dumps( { "data": self.__data, "error":self.error, }, cls = DjangoJSONEncoder) def __set_container(self, val): pass _container = property(__get_container, __set_container) def __init__(self, *args, **kwargs): kwargs["mimetype"] = "application/javascript" if "data" in kwargs: self.data = kwargs.pop("data") if "error" in kwargs: self.error = kwargs.pop("error") super(JsonResponse, self).__init__(*args, **kwargs)