0

我想使用 Django Rest Framework 应用分页。我有以下 GET 视图。

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)

如何对数据进行分页?目前它返回所有数据。我已经阅读了 API 文档以及 SO 上的各种线程,因此我在 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' 

}

但似乎没有任何效果。我的 URL 是http://127.0.0.1:8000/post/?page=1,我目前有 5-6 条记录,因此我设置PAGE_SIZE为 1,但它仍然无法按预期工作。如何对我的数据记录进行分页?

编辑

以下是我的模型

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)

预期响应

{
        "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....],
}
4

1 回答 1

0

取消注释设置中的两行代码

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

编辑

在序列化程序中

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',)

在视图中

response_data = ModelASerializer(objs).data

*其中 objs 是 ModelA 的查询集

于 2015-05-15T06:20:39.430 回答