Urls.py:
app_name = 'main'
urlpatterns = [
path('',include(router.urls)),
path('player_id=<str:player>%season_id=<str:season>',views.MatchesList.as_view())
]
Views.py
class MatchesList(generics.ListAPIView):
serializer_class = MatchesSerializer
permissions = (IsAuthenticated)
def get_queryset(self):
player = self.kwargs['player']
season = self.kwargs['season']
if season is None:
queryset = Matches.objects.filter(player=player).all()
else:
queryset = Matches.objects.filter(player=player,season=season).all()
return queryset
Is there any way to request without the parameter 'season'? Something like that:
app_name = 'main'
urlpatterns = [
path('',include(router.urls)),
path('player_id=<str:player>',views.MatchesList.as_view())
]
CodePudding user response:
Sure. In DRF you can/should use filtering for this.
In urls.py
you'll have something like this:
path('matches/<str:player_id>/', views.MatchesList.as_view())
And your URL will look like:
https://yourhost.com/matches/42/?season=spring
For season
you'll have to implement a filter (there is a bunch of ways to do it). Filters are optional — if you'll not pass the ?season=something
part of the URL, it just will not be applied.