Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialY B
14,136 PointsDjango rest framework, override get queryset
I think this looks ok, but I can't seem to pass the challenge for some reason?
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.views import APIView
from . import models
from . import serializers
class GameListCreate(generics.ListCreateAPIView):
queryset = models.Game.objects.all()
serializer_class = serializers.GameSerializer
class GameScoresList(generics.ListAPIView):
queryset = models.Score.objects.all()
serializer_class = serializers.ScoreSerializer
def get_queryset(self, game_pk):
return self.queryset.filter(id=self.kwargs.get('game_pk'))
4 Answers
Tatiana Vasilevskaya
Python Web Development Techdegree Graduate 28,600 PointsThere are two issues here. The first one is that get_queryset
method doesn't accept game_pk
argument.
The second thing is the queryset filtering. The get_queryset
method has to filter the queryset to only have scores for the game with the specific pk. The current implementation:
def get_queryset(self, game_pk):
return models.Score.objects.filter(id=self.kwargs.get('game_pk'))
i.e. you filter by the score id, but you have to filter by game.
Anders Prytz
26,193 PointsHi Aurélien!
You would have to use 'game' and not 'game_id' (found out after some attempts). Such that your filter would look like: game=self.kwargs.get('game_pk')
Otherwise it looks correct:)
Joshua Kaufman
12,035 PointsThis should be marked as the answer.
Aurélien Debord
18,114 PointsI tried this too:
def get_queryset(self):
return self.queryset.filter(game_id=self.kwargs.get('game_pk'))
Can anyone tells me what is wrong there?
Aurélien Debord
18,114 Pointsthanks Anders
Bas Kuunk
21,308 PointsBas Kuunk
21,308 PointsAccording to your comment, this should work:
But it does not. Neither does my code:
Can anyone tell me what is wrong with either of these?