Skip to content

Instantly share code, notes, and snippets.

@sourabhv
Created October 18, 2013 17:08
Show Gist options
  • Save sourabhv/7044640 to your computer and use it in GitHub Desktop.
Save sourabhv/7044640 to your computer and use it in GitHub Desktop.
Django JSONSerializer to serialize Django model's fields and properties into JSON
from StringIO import StringIO
from django.core.serializers.json import Serializer
class JSONSerializer(Serializer):
'''
JSON serialize to serialize db fields and properties
Example:
>>> JSONSerializer().serialize(Model.objects.all(), ('field1', 'field2',))
'''
def serialize(self, queryset, attributes, **options):
self.options = options
self.stream = options.get("stream", StringIO())
self.start_serialization()
self.first = True
for obj in queryset:
self.start_object(obj)
for field in attributes:
self.handle_field(obj, field)
self.end_object(obj)
if self.first:
self.first = False
self.end_serialization()
return self.getvalue()
def handle_field(self, obj, field):
self._current[field] = getattr(obj, field)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment