Created
October 2, 2015 15:20
-
-
Save inakidelamadrid/ad1eabfcae70318289b2 to your computer and use it in GitHub Desktop.
Django Views - How to serialize an array of django objects into flat JSON (exclude model, pk, and fields node)
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
import json | |
from django.forms.models import model_to_dict | |
from django.core.serializers.json import DjangoJSONEncoder | |
events = [some django model query] | |
# convert every object into a dictionary | |
data = map(lambda evt: model_to_dict(evt), events) | |
return HttpResponse(json.dumps(data, cls=DjangoJSONEncoder), | |
content_type="application/json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This serialization method can't serialize any type of FileField.