【问题标题】:How to return custom JSON output in Django REST Framework如何在 Django REST Framework 中返回自定义 JSON 输出
【发布时间】:2020-03-01 20:32:31
【问题描述】:

我正在尝试返回具有以下结构的自定义 json

[ { 
'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"}]
}]

以下是我的代码:

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

电流输出

[
    {},
    {},
    {}
]

如何修改我的 GamesSerializer 以返回如上所示的自定义输出。

【问题讨论】:

    标签: django python-3.x django-rest-framework


    【解决方案1】:

    您可以将您的响应类从 DRF ModelViewSet 转换为 ViewSet。 然后,您可以在返回响应之前进一步解析您的数据,方法是覆盖 retrieve

    这里提到:https://www.django-rest-framework.org/api-guide/viewsets/#example

    【讨论】:

      【解决方案2】:

      对于任何试图找到答案的人,我使用Viewset 而不是ModelViewSet 所建议的@jay-vasant,然后使用上面的overrode list 方法来自定义我想要的输出。 这是我更新后的GamesViewSet

      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)
      

      输出我之后到达那里。

      [
          [{'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"}]
       ]
      

      现在已经接近我想要的了。

      【讨论】:

        猜你喜欢
        • 2016-09-07
        • 2019-02-02
        • 2020-04-09
        • 1970-01-01
        • 2016-05-03
        • 2017-10-16
        • 1970-01-01
        • 1970-01-01
        • 2019-08-19
        相关资源
        最近更新 更多