from django.core.serializers.json import DateTimeAwareJSONEncoder
from django.core import serializers
from django.db.models.query import QuerySet
from django.db.models import Model
from django.utils import simplejson

class ModelAwareJSONEncoder(DateTimeAwareJSONEncoder):
    def default(self, o):
        if isinstance(o, QuerySet):
            return serializers.serialize('python', o)
        if isinstance(o, Model):
            return serializers.serialize('python', [o])[0]
        else:
            return super(ModelAwareJSONEncoder, self).default(o)

def serialize(object, **options):
    return simplejson.dumps(object, cls=ModelAwareJSONEncoder, **options)