Reputation: 31
i'm new in django and i have a little problem in the pagination of objects.
my model:
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'
my view:
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)
my pagination.py:
class SmallPagesPagination(PageNumberPagination):
page_size = 6
The problem is that it does not put me at all 6 objects per page. if i do not override the get method, it works.
Upvotes: 3
Views: 8114
Reputation: 177
One way is to override the MyOffsetPagination class like this
from rest_framework.pagination import LimitOffsetPagination
class MyOffsetPagination(LimitOffsetPagination):
default_limit = 20
max_limit = 1000
and then add this in your view
pagination_class = MyOffsetPagination
Don't forget to add this in you settings
'DEFAULT_PAGINATION_CLASS': (
'rest_framework.pagination.PageNumberPagination'),
'PAGE_SIZE': (50)
Upvotes: -1
Reputation: 281
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)
Upvotes: 9