如何使用Django应用程序中的request.query_params.get设计URL以获取视图中的数据。
以下是我的观点:
class BattleListByYear(generics.ListAPIView):
serializer_class = BattleSerializer
def get_queryset(self):
queryset = Battles.objects.all()
year = self.request.query_params.get('year',None)
if year is not None:
queryset = queryset.filter(year__exact=year)
return queryset
答案 0 :(得分:0)
:
url(r'^year/(?P\d+)/$', BattleListByYear.as_view(), name='battle-list-by-year'),
在您的观看中,您可以通过self.kwargs ['年']访问它:
class BattleListByYear(generics.ListAPIView):
serializer_class = BattleSerializer
def get_queryset(self):
queryset = Battles.objects.all()
year = self.kwargs['year']
if year is not None:
queryset = queryset.filter(year__exact=year)
return queryset
来源:http://www.django-rest-framework.org/api-guide/filtering/