1

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

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)

This is my pagination class.

class StandardPagesPagination(PageNumberPagination):
      page_size = 10

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.

cvsrt
  • 357
  • 4
  • 19

1 Answers1

2

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())
        ...

So filter_queryset is the missing piece here.

loicgasser
  • 1,403
  • 12
  • 17