[Django]-How do I pass a JSON object to FullCalendar from Django (by serializing a model)?

8👍

Don’t use Django’s built-in serializers for this. I almost never use them – they are very inflexible.

Luckily, it’s very simple to serialize the content yourself.

from django.utils import simplejson
from django.core.serializers.json import DjangoJSONEncoder

events = Event.objects.filter(
              user=request.user, start__gte=start, end__lte=end
         ).values('id', 'title', 'start', 'end')
data = simplejson.dumps(list(events), cls=DjangoJSONEncoder)

Here I’m just getting a dictionary from the queryset via values, and passing it to simplejson to encode the select list of fields. I need to use the DjangoJSONEncoder as by default json doesn’t know about datetimes, so this encoder adds that functionality.

Leave a comment