python - Representing more than single object/list CRUD ops with Django REST Framework ViewSets -
i've been writing game picking style webapp django , decided implement views api endpoints drf, give me more flexibility when comes frontend approaches. have basic serializers , viewsets each of models, , can browse them (excellent) browsable api. here couple:
class sheetserializer(serializers.hyperlinkedmodelserializer): user = userserializer(read_only = true) league = leagueserializer(read_only = true) picks = serializers.hyperlinkedrelatedfield( source='pick_set', many=true, view_name='pick-detail', read_only = true ) class meta: model = sheet fields = ('url', 'id', 'league_week', 'user', 'league', 'picks') class gameserializer(serializers.hyperlinkedmodelserializer): class meta: model = game fields = ('url', 'home_team', 'away_team', 'week', 'home_team_score', 'away_team_score') class pickserializer(serializers.hyperlinkedmodelserializer): sheet = sheetserializer() game = gameserializer() class meta: model = pick fields = ('url', 'sheet', 'amount', 'spread', 'pick_type', 'pick_team', 'game')
with respective viewsets:
class pickviewset(viewsets.modelviewset): queryset = pick.objects.all() serializer_class = pickserializer class gameviewset(viewsets.modelviewset): queryset = game.objects.all() serializer_class = gameserializer class sheetviewset(viewsets.modelviewset): queryset = sheet.objects.all() serializer_class = sheetserializer
what i'm having trouble how represent more complex endpoints single-object or list of same-type object crud operations. example, have regular django view matchups pulls users sheet (collection of picks), users sheet, , displays nested picks against each other. i'm planning display other data on same page other users in whatever league they're part of. implementation user + opponent data in vanilla django looks this:
class matchupdetail(detailview): template_name = 'app/matchups.html' context_object_name = 'pick_sheet' def get_object(self): #logic find , return object def get_opponent(self,username,schedule,week, **kwargs): #logic find , return opponent in matchup def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) #logic pull opponents details , set them in context
i've been struggling on how represent in viewset. regular django view, it's easy write get_object (or get_list) get_context_data, include user's data + other desired objects database, , pass of template. currently, (early) api endpoint version of above looks this:
class matchupviewset(viewsets.readonlymodelviewset): serializer_class = sheetserializer def get_queryset(self): user = self.request.user return sheet.objects.filter(user=self.request.user) def list(self, request, format=none): sheets = self.get_queryset() serializer = sheetserializer(sheets, many=true, context={'request': request}) return response(serializer.data) def retrieve(self, request, pk, format=none): sheet = sheet.objects.get(user=self.request.user, league_week=pk) serializer = sheetserializer(sheet, context={'request':request}) return response(serializer.data)
this code works, returns limited subset of objects- single users sheet(s). aim in viewset return list of matchups (whi collection of user sheets) given league list()
, given league + week retrieve()
, i'm not sure how return data. normal django context variable, data include can named in manner like- how goal of collecting , returning arbitrary number of potentially different objects accomplished drf? edit: wasn't clear initially- i've gotten nested representation working, i'm wondering if there's way name/label different serialized objects in manner information in normal context can set name
say want pass requesting users serialized sheet, along opponents sheet , other players sheets particular league , week. logic determine belongs user, belongs opponent, , other players' have live in frontend code?
also, how configure router list() method requires url parameter (for league)? anytime change registration
router.register(r'matchup', matchupviewset, base_name = 'matchup')
to
router.register(r'matchup/(?p<league>[0-9]+)/$', matchupviewset, base_name = 'matchup')
the endpoint disappears browsable api root.
how goal of collecting , returning arbitrary number of potentially different objects accomplished drf?
i'm not sure question here. assume you'll want nested representations explained here.
the endpoint disappears browsable api root.
this because browsable api doesn't know how resolve kwargs. since no url matches, won't display link
@admin
ReplyDeleteThese are the values that we hold central to our practice. We create beautiful smiles as we focus on the overall health of your mouth. We encourage you to call or email us with any questions or concerns that you have so that we can better serve you.
Regards,
Dentist In unionville, CT