How can pass my data into this particular serializer?

33 views Asked by At

I'm currently working with data that looks like:

{ "events": [] }

But, I'd like to transform it to:

{ "events":
     "added-to-calendar": [],
     "confirmed": []
 }

I've written out a serializer that looks like:

class CalendarFeedSerializer(serializers.Serializer):
    """ Serializer for the Calendar Feed """

    added_to_calendar = EventSerializer(many=True, read_only=True)
    confirmed = EventSerializer(many=True, read_only=True)

    class Meta:
        model = Event

But, I'm not entirely sure how to pass my data into this Serializer.

Here's the ListAPIView that I am working with to achieve { "events": [] }:

class CalendarFeedView(generics.ListAPIView):

    serializer_class = serializers.EventSerializer

    def get_queryset(self):
        user = get_user_model().objects.get(id=self.request.user.id)
        added_to_calendar_events = user.events_in_calendar()
        confirmed_events = user.events_confirmed()
        return added_to_calendar_events | confirmed_events

But, as you can see, I am using the base EventSerializer that I created to transform this list of Event's. What I want, is to somehow pass added_to_calendar_events and confirmed_events to my CalendarFeedSerializer so that I can have the desired data structure

{ "events":
     "added-to-calendar": [],
     "confirmed": []
 }
0

There are 0 answers