How to pass a JSON object to FullCalendar from Django (by serializing the model)?

FullCalendar supports the use of the JSON object through AJAX for its events, this can be done during initialization or later as follows:

$('#calendar').fullCalendar('addEventSource', "/{{ user }}/events/" );

The serialization itself in my Django view is as follows:

...
events = Event.objects.filter(user=request.user, start__gte=start, end__lte=end)
message = serializers.serialize("json", events, ensure_ascii=False)
...

The returned JSON object is as follows:

[{"pk": 2, "model": "main.event", "fields": {"url": null, "start": "2010-10-09 08:30:00", "end": "2010-10-09 10:30:00", "user": 1, "title": "sdf"}}, {"pk": 3, "model": "main.event", "fields": {"url": null, "start": "2010-10-03 08:30:00", "end": "2010-10-03 12:00:00", "user": 1, "title": "sdf2"}}]

The following variables are used in the Fullcalendar event: id, title, start, end, allDay and url.

I think FullCalendar is getting my JSON object right now (not sure how to check), but it is probably not acceptable, how can I make it acceptable for FullCalendar? It probably also looks something like this:

[{id: 1, title: 'Title1', start: new Date(2010, 10, 3, 8, 30), end: new Date(2010, 10, 3, 12, 0), allDay: false, url: false}]

or

[{"id": 1, "title": 'Title1', "start": new Date(2010, 10, 3, 8, 30), "end": new Date(2010, 10, 3, 12, 0), "allDay": false, "url": false}]

Or even something else, not sure.

, , JSON, , JSON, ?

+3
1

Django. - .

, .

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)

values simplejson . DjangoJSONEncoder, json , .

+8

Source: https://habr.com/ru/post/1769140/


All Articles