Created
October 18, 2013 17:08
-
-
Save sourabhv/7044640 to your computer and use it in GitHub Desktop.
Django JSONSerializer to serialize Django model's fields and properties into JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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