简体   繁体   中英

Pagination in Django Rest Framework

I want to apply pagination using Django Rest Framework. I've the following GET view.

def get(self,request,format=None):
    response_data = []
    status = request.GET.getlist('status') or None
    location = request.GET.getlist('location') or None
    category = request.GET.getlist('category') or None
    min_price = request.GET.get('min_price') or None
    max_price = request.GET.get('max_price') or None

    ModelA_obj = Model.objects.all()
    if status : ModelA_obj = ModelA_obj.filter(status__in=status)
    if location : ModelA_obj = ModelA_obj.filter(location__in=location)
    if min_price : ModelA_obj = ModelA_obj.filter(minimum_expected_price__gte=int(min_price))
    if max_price : ModelA_obj = ModelA_obj.filter(buyout_price__lte=int(max_price))
    if category : 
        category_List = ModelB.objects.filter(subcategory__name__in=category).values_list('column',flat=True)
        ModelA_obj = ModelA_obj.filter(id__in=category_List)

    for obj in ModelA_obj:
        #this fetches me all the details from different models
        details = ModelA.get_details(obj) 
        response_data.append(details)
    return Response(response_data)

How do I paginate the data? Currently it returns all the data. I've read the API Documentation, and various threads on SO as well, hence I applied the following in settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    ),

    #'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    #'PAGE_SIZE': 2,
    'PAGINATE_BY': 1, 
    'PAGINATE_BY_PARAM': 'page' 

}

But nothing seems to work. My URL is http://127.0.0.1:8000/post/?page=1 , and I currently have 5-6 records in it, hence I set PAGE_SIZE as 1, but it still not working as expected. How do I paginate my data records?

EDIT

Following are my models

class ModelA(models.Model):
    col1 = models.CharField(max_length=255)
    Col2 = models.TextField(blank=True,null=True)
    def get_details(obj):
        #get data from ModelB and ModelC referenced with FK `obj`


class ModelB(models.Model):
    col3 = models.CharField(max_length=255)
    fk_A = models.ForeignKEy(ModelA)


class ModelC(models.Model):
    col4 = models.CharField(max_length=255)
    fk_A = models.ForeignKEy(ModelA)

Expected Response

{
        "col1": value,
        "col2": value,
        "modelb_details": [col3_value1, col3_value2....],
        "modelc_details": [col4_value1, col4_value2....],
},
{
        "col1": value,
        "col2": value,
        "modelb_details": [col3_value1, col3_value2....],
        "modelc_details": [col4_value1, col4_value2....],
}

Uncomment the two lines of code in your settings

'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 2,

EDIT

In serializers

class ModelASerializer(serializers.ModelSerializer):
   modelb_details=ModelBSerializer(source="model_b")
   modelc_details=ModelCSerializer(source="model_c")

   class Meta:
       model = ModelA

class ModelBSerializer(serializers.ModelSerializer):

   class Meta:
       model = ModelB
       fields = ('col3',)

class ModelCSerializer(serializers.ModelSerializer):

   class Meta:
       model = ModelC
       fields = ('col4',)

In views

response_data = ModelASerializer(objs).data

*where objs is an queryset of ModelA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM