2

I am trying to return custom json with the following structure

[ { 
'yesterday': [{'teams': "team a -team b", 'start_time': "0: 11", 'pick': "X2", 'score': "1:4", 'odds': 1.25, 'won_or_lost': "won", 'date': "2019-01-8"}],

'today': [{'teams': "team a -team b", 'start_time': "0: 11", 'pick': "X2", 'score': "1:4", 'odds': 1.25, 'won_or_lost': "won", 'date': "2019-01-8"}],

'tomorrow': [{'teams': "team a -team b", 'start_time': "0: 11", 'pick': "X2", 'score': "1:4", 'odds': 1.25, 'won_or_lost': "won", 'date': "2019-01-8"}]
}]

The following is my code:

serializer.py

class GamesSerializer(serializers.Serializer):
    class Meta:
        model = AllGames
        fields = ('teams', 'start_time', 'pick',
                  'score', 'odds', 'won_or_lost', 'date')


class GamesViewSet(viewsets.ModelViewSet):
    today_date = date_picker

    yesterday = AllGames.objects.filter(
        date=today_date(-1)).order_by('start_time', 'teams')
    today = AllGames.objects.filter(
        date=today_date(0)).order_by('start_time', 'teams')
    tomorrow = AllGames.objects.filter(
        date=today_date(1)).order_by('start_time', 'teams')

    queryset = [yesterday, today, tomorrow]
    serializer_class = GamesSerializer

Current Output

[
    {},
    {},
    {}
]

How can I modify my GamesSerializer to return the custom output as shown above.

2 Answers 2

2

You can convert your response class from DRF ModelViewSet to ViewSet. Then you can further parse your data before returning response, by overriding retrieve

As mentioned here: https://www.django-rest-framework.org/api-guide/viewsets/#example

Sign up to request clarification or add additional context in comments.

Comments

0

For anyone trying to find the answer for this I used Viewset instead of ModelViewSet as suggested by @jay-vasant and then overrode list method above to customize the output like I wanted. This is my GamesViewSet after update.

class GamesViewSet(viewsets.ViewSet):
def list(self, request):
    today_date = date_picker

    yesterday = AllGames.objects.filter(
        date=today_date(-1)).order_by('start_time', 'teams')
    today = AllGames.objects.filter(
        date=today_date(0)).order_by('start_time', 'teams')
    tomorrow = AllGames.objects.filter(
        date=today_date(1)).order_by('start_time', 'teams')

    queryset = [yesterday, today, tomorrow]
    games = []
    for day in queryset:
        serializer = GamesSerializer(day, many=True)
        games.append(serializer.data)
    return Response(games)

Output I get there after.

[
    [{'teams': "team a -team b", 'start_time': "0: 11", 'pick': "X2", 'score': "1:4", 'odds': 1.25, 'won_or_lost': "won", 'date': "2019-01-8"}],

    [{'teams': "team a -team b", 'start_time': "0: 11", 'pick': "X2", 'score': "1:4", 'odds': 1.25, 'won_or_lost': "won", 'date': "2019-01-8"}],
    [{'teams': "team a -team b", 'start_time': "0: 11", 'pick': "X2",'score': "1:4", 'odds': 1.25, 'won_or_lost': "won", 'date': "2019-01-8"}]
 ]

Which is now close to what I wanted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.