Soubhagya Pradhan
Soubhagya Pradhan

Reputation: 35

django rest framework calculate/modify result before sending to browser

view:

class ChartAPIView(RetrieveAPIView):
    serializer_class = ChartSerializer
    queryset = Chart.objects.prefetch_related('attendees__person').all()

    def get_object(self):
        user = self.request.user
        last_updated = user.profile.data_last_updated
        user_events = self.queryset.filter(user=user).order_by('-created')

        data = {
            'total': user_events.count(),
            'last_updated': last_updated,
            "chart_data": user_events
        }
        return type('DashboardData', (), data)()

Serializer:

class EventSerializer(serializers.ModelSerializer):
    class Meta:
        model = Event
        fields = ('duration', 'attendees', 'created')

class DashboardSerializer(serializers.Serializer):
    total = serializers.IntegerField(read_only=True)
    last_updated = serializers.DateTimeField(read_only=True)
    chart_data = EventSerializer(read_only=True, many=True)

Result:

{
    "total": 2,
    "last_updated": "2020-09-22 04:49:25",
    "chart_data": [
        {
            "title": "Daily Stand-up",
            "organizer": "[email protected]",
            "duration": "0:30",
            "attendees": "[email protected], [email protected]",
            "created": "2020-08-25 06:11:54",
            "updated": "2020-09-17 04:50:25"
        },
        {
            "title": "Daily Stand-up",
            "organizer": "[email protected]",
            "duration": "0:30",
            "attendees": "[email protected], [email protected]",
            "created": "2020-08-25 06:11:54",
            "updated": "2020-09-17 04:50:25"
        },
        {
            "title": "Daily Stand-up",
            "organizer": "[email protected]",
            "duration": "0:30",
            "attendees": "[email protected], [email protected]",
            "created": "2020-08-25 06:11:54",
            "updated": "2020-09-17 04:50:25"
        }
]

Here is my code and results i am getting . But, before sending result to browser i wants to do some modification

Is there any way we can achive that.

Basically i wants to modify user_events data by passing it to a utils function.

Please have a look.

Expected result:

[{'created': '2020-08-24', 'duration': 510}, {'created': '2020-08-25', 'duration': 1260}]

I have already the function which is converting to it but, as the user_events is a query set i amgetting error while getting the key.

Upvotes: 0

Views: 115

Answers (1)

dVeza
dVeza

Reputation: 552

If it's fine to you to change the EventSerializer for all responses, I suggest you use a SerializerMethodField (https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield ):

obj is an instance of each Event, and get_duration will be invoked instead of the default duration to change the format as you wanted for the field duration when serializing.

class EventSerializers(serializers.ModelSerializer):
    duration  = serializers.SerializerMethodField()

    def get_duration(self, obj):
        return your_custom_util(obj.duration)

Upvotes: 1

Related Questions