我是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)
我的pagination.py:
class SmallPagesPagination(PageNumberPagination):
page_size = 6
问题是它并没有让我每页都有6个对象。如果我不覆盖get方法,它就可以工作。
答案 0 :(得分:3)
<强> 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)
答案 1 :(得分:-1)
一种方法是重写MyOffsetPagination类 像这样
from rest_framework.pagination import LimitOffsetPagination
class MyOffsetPagination(LimitOffsetPagination):
default_limit = 20
max_limit = 1000
然后将其添加到您的视图中
pagination_class = MyOffsetPagination
不要忘记在您的设置中添加
'DEFAULT_PAGINATION_CLASS': (
'rest_framework.pagination.PageNumberPagination'),
'PAGE_SIZE': (50)