覆盖序列化程序类中的查询集

时间:2019-11-12 09:29:47

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

我有三种模型:

game:
id,home_team_id, away_team_id, season_type<br>
team:
id,name
team_standing:
id, team_id, season_type, win, loss

我曾经在drf序列化程序中使用过急切的加载方式

 @classmethod
 def setup_eager_loading(cls, queryset):
     """ Perform necessary eager loading of data. """
     # select_related for "to-one" relationships
     queryset = queryset.select_related('home_team', 'away_team').prefetch_related('home_team__team_standings', 'away_team__team_standings')
    return queryset

我需要包含团队详细信息的游戏列表,包括team_standings:

[
 {
   id:1,
   home_team_id:1,
   away_team_id:2,
   season_type:1,
   home_team:{
    id: 1 (it will be id of team model),
    name: GS,
    team_standing:{
    id:team standing model id,
    season_type: season_type value but it must be same as of game season_type,
    team_id: team id,
    win:3
    }
   },
   away_team:{
   id: 1 (it will be id of team model),
   name: INA,
   team_standing:{
    id:team standing model id,
    season_type: season_type value but it must be same as of game season_type,
    team_id: team id,
    win:2
    }
   }
  }
 ]

但是我得到了不同季节类型的所有球队常规赛记录。

0 个答案:

没有答案