gpt4 book ai didi

django - django rest 中的分页,ListAPIView

转载 作者:行者123 更新时间:2023-12-05 03:07:27 24 4
gpt4 key购买 nike

我是 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'

我的看法:

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)

我的分页.py:

class SmallPagesPagination(PageNumberPagination):  
page_size = 6

问题是它并没有把我放在每页 6 个对象中。如果我不覆盖 get 方法,它就可以工作。

最佳答案

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)

关于django - django rest 中的分页,ListAPIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47225016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com