Skip to content

Instantly share code, notes, and snippets.

@inakidelamadrid
Created October 2, 2015 15:20
Show Gist options
  • Save inakidelamadrid/ad1eabfcae70318289b2 to your computer and use it in GitHub Desktop.
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)
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")
@inakidelamadrid
Copy link
Author

This serialization method can't serialize any type of FileField.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment