Django Rest Framework分页和过滤 [英] Django Rest Framework pagination and filtering

查看:95
本文介绍了Django Rest Framework分页和过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi guys i am trying to make filtering with pagination but i cannot get the result i want.

这是我在views.py中的功能.

This is my function in 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时,它不起作用.

And this is the json i got but when i write localhost/api/games?platform=pc or localhost/api/games?categories=Action it is not working.

{
    "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"...

您能帮我解决这个问题吗?我找不到任何解决方案.

Can you help me guys with this problem?I couldn't find any solution.

推荐答案

如果覆盖 get 方法,则必须确保您知道原始实现的样子.

If you override the get method you have to make sure you know how the original implementation looks like.


    def get(self,request,*args,**kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        ...

所以 filter_queryset 是这里缺少的部分.

So filter_queryset is the missing piece here.

这篇关于Django Rest Framework分页和过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆