Django Rest Framework分页和过滤

问题描述

嗨,我正在尝试使用分页进行过滤,但是我无法获得想要的结果。

这是我在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 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","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",{
            "game_id": 11,"title": "Marvel's Avengers","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",{
            "game_id": 24,"title": "The Suicide of Rachel Foster","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","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是这里丢失的部分。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...