Django Rest Framework 分页和过滤
Django Rest Framework pagination and filtering
大家好,我正在尝试使用分页进行过滤,但我无法获得想要的结果。
这是我在 views.py 中的功能。
class OyunlarList(generics.ListAPIView):
# queryset = Oyunlar.objects.all()
pagination_class = StandardPagesPagination
filter_backends = [DjangoFilterBackend]
filterset_fields = ['categories__name', 'platform']
# serializer_class = OyunlarSerializer
def get_queryset(self):
queryset=Oyunlar.objects.all()
oyunlar=OyunlarSerializer.setup_eager_loading(queryset)
return oyunlar
def get(self,request,*args,**kwargs):
queryset = self.get_queryset()
serializer=OyunlarSerializer(queryset,many=True)
page=self.paginate_queryset(serializer.data)
return self.get_paginated_response(page)
这是我的分页class。
class StandardPagesPagination(PageNumberPagination):
page_size = 10
这是我得到的 json 但是当我写 localhost/api/games?platform=pc 或 localhost/api/games?categories=Action 时它不起作用。
{
"count": 18105,
"next": "http://127.0.0.1:8000/api/oyunlar?categories__name=&page=2&platform=pc",
"previous": null,
"results": [
{
"game_id": 3,
"title": "The Savior's Gang",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP3729-CUSA23817_00-THESAVIORSGANG00/1599234859000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [],
"release_date": null
},
{
"game_id": 8,
"title": "Spellbreak",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP0795-CUSA18527_00-SPELLBREAK000000/1599612713000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [],
"release_date": null
},
{
"game_id": 11,
"title": "Marvel's Avengers",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP0082-CUSA14030_00-BASEGAME0001SIEE/1599653581000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [],
"release_date": null
},
{
"game_id": 24,
"title": "The Suicide of Rachel Foster",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP8923-CUSA19152_00-DAEEUTSORF000001/1599610166000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [
{
"category_id": 2,
"name": "Casual"
},
{
"category_id": 5,
"name": "İndie"
},
{
"category_id": 8,
"name": "Adventure"
}
],
"release_date": "2020-09-09"
},
{
"game_id": 25,
"title": "Takotan",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP2005-CUSA24716_00-TKTN000000000000/1599610166000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [
{
"category_id": 1,
"name": "Action"
},
{
"category_id": 12,
"name": "Arcade"
},
{
"category_id": 13,
"name": "Shooter"
}
],
"release_date": "2020-09-09"...
你们能帮我解决这个问题吗?我找不到任何解决方案。
如果您覆盖 get
方法,您必须确保您知道原始实现的样子。
def get(self,request,*args,**kwargs):
queryset = self.filter_queryset(self.get_queryset())
...
所以 filter_queryset
是这里缺少的部分。
大家好,我正在尝试使用分页进行过滤,但我无法获得想要的结果。
这是我在 views.py 中的功能。
class OyunlarList(generics.ListAPIView):
# queryset = Oyunlar.objects.all()
pagination_class = StandardPagesPagination
filter_backends = [DjangoFilterBackend]
filterset_fields = ['categories__name', 'platform']
# serializer_class = OyunlarSerializer
def get_queryset(self):
queryset=Oyunlar.objects.all()
oyunlar=OyunlarSerializer.setup_eager_loading(queryset)
return oyunlar
def get(self,request,*args,**kwargs):
queryset = self.get_queryset()
serializer=OyunlarSerializer(queryset,many=True)
page=self.paginate_queryset(serializer.data)
return self.get_paginated_response(page)
这是我的分页class。
class StandardPagesPagination(PageNumberPagination):
page_size = 10
这是我得到的 json 但是当我写 localhost/api/games?platform=pc 或 localhost/api/games?categories=Action 时它不起作用。
{
"count": 18105,
"next": "http://127.0.0.1:8000/api/oyunlar?categories__name=&page=2&platform=pc",
"previous": null,
"results": [
{
"game_id": 3,
"title": "The Savior's Gang",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP3729-CUSA23817_00-THESAVIORSGANG00/1599234859000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [],
"release_date": null
},
{
"game_id": 8,
"title": "Spellbreak",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP0795-CUSA18527_00-SPELLBREAK000000/1599612713000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [],
"release_date": null
},
{
"game_id": 11,
"title": "Marvel's Avengers",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP0082-CUSA14030_00-BASEGAME0001SIEE/1599653581000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [],
"release_date": null
},
{
"game_id": 24,
"title": "The Suicide of Rachel Foster",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP8923-CUSA19152_00-DAEEUTSORF000001/1599610166000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [
{
"category_id": 2,
"name": "Casual"
},
{
"category_id": 5,
"name": "İndie"
},
{
"category_id": 8,
"name": "Adventure"
}
],
"release_date": "2020-09-09"
},
{
"game_id": 25,
"title": "Takotan",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP2005-CUSA24716_00-TKTN000000000000/1599610166000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [
{
"category_id": 1,
"name": "Action"
},
{
"category_id": 12,
"name": "Arcade"
},
{
"category_id": 13,
"name": "Shooter"
}
],
"release_date": "2020-09-09"...
你们能帮我解决这个问题吗?我找不到任何解决方案。
如果您覆盖 get
方法,您必须确保您知道原始实现的样子。
def get(self,request,*args,**kwargs):
queryset = self.filter_queryset(self.get_queryset())
...
所以 filter_queryset
是这里缺少的部分。