【发布时间】: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