django 休息中的分页,ListAPIView

Thi*_*uge 3 django django-rest-framework

我是 django 的新手,在对象分页方面遇到了一些问题。

我的模型:

class FacebookBien(models.Model):
uid = models.IntegerField(primary_key=True)
ref_agence = models.IntegerField()
loyer = models.FloatField()
prix = models.FloatField()
ville = models.CharField(max_length=200)
code_postal = models.CharField(max_length=200)
ref_type_bien = models.IntegerField()
surface_totale = models.FloatField()
source = models.CharField(max_length=200)
nombre_de_pieces = models.IntegerField()
date_modification = models.DateTimeField()

class Meta:
    managed = False
    # db_table = 'public\".\"bien_recherche'
    db_table = 'recette_facebook\".\"vue_facebook_bien_recherche'
Run Code Online (Sandbox Code Playgroud)

我的看法:

class BienAgence(generics.ListAPIView):
queryset = FacebookBien.objects
pagination_class = SmallPagesPagination

def get(self, request, *args, **kwargs):
    res = request.GET
    if FacebookBien.objects.filter(ref_agence=int(kwargs['ref_agence'])).exists():
        toto = self.queryset.filter(ref_agence=int(kwargs['ref_agence']))
        if (res['type'] == 'bien'):
            toto = toto.filter(prix__gte=int(res['p_mini']))
            toto = toto.filter(prix__lte=int(res['p_maxi']))
            if (res['prix'] == 'croissant'):
                toto = toto.order_by('prix')
            elif (res['prix'] == 'decroissant'):
                toto = toto.order_by('-prix')
        else:
            toto = toto.filter(loyer__gte=int(res['p_mini']))
            toto = toto.filter(loyer__lte=int(res['p_maxi']))
            if (res['prix'] == 'croissant'):
                toto = toto.order_by('loyer')
            elif (res['prix'] == 'decroissant'):
                toto = toto.order_by('-loyer')
        if (res['surface'] == 'croissant'):
            toto = toto.order_by('surface_totale')
        elif (res['surface'] == 'decroissant'):
            toto = toto.order_by('-surface_totale')
        elif (res['date'] == 'croissant'):
            toto = toto.order_by('date_creation')
        elif (res['date' == 'decroissant']):
            toto = toto.order_by('-date_creation')
        toto = toto.filter(surface__gte=int(res['s_mini']))
        toto = toto.filter(surface__lte=int(res['s_maxi']))
        serializer = FacebookBienSerializer(toto, many=True) # noqa
        return Response(serializer.data)
    else:
        return Response(None)
Run Code Online (Sandbox Code Playgroud)

我的分页.py:

class SmallPagesPagination(PageNumberPagination):  
page_size = 6
Run Code Online (Sandbox Code Playgroud)

问题是它并没有在每页上放置 6 个对象。如果我不重写 get 方法,它就会起作用。

Sam*_*enc 8

my_view.py

class BienAgence(generics.ListAPIView):
    queryset = FacebookBien.objects
    pagination_class = SmallPagesPagination

    def get(self, request, *args, **kwargs):
        ...
        serializer = FacebookBienSerializer(toto, many=True)
        page = self.paginate_queryset(serializer.data)
        return self.get_paginated_response(page)
Run Code Online (Sandbox Code Playgroud)