如何在djangorest框架中为从google big query接收到的数据添加分页

ewm0tg9j  于 2023-01-21  发布在  Go
关注(0)|答案(1)|浏览(81)

我有一些从google big query接收到的数据,我尝试在django rest框架中对这些数据进行分页,而且我没有使用序列化器来实现正确的输出,有什么理想的方法来添加分页,就像我在底部提到的那样?

class CashFlowList(generics.ListAPIView):
    permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
    pagination_class = PageNumberPagination

    @staticmethod
    def cash_flow_list(self, rows, info, *args, **kwargs):
        data = {'invoices': rows}
        message = {'info': info, 'data': data}
        return message

    def get(self, request):
    
        try:
   
  
            dataset = 'dummy_dataset'

    

            get_cashflow_query = f" SELECT  ref_no AS invoice_number, party_name AS customer_name,credit_amount, pending_amount," \
                             f" DATE_ADD(date, INTERVAL credit_period DAY) AS due_date, DATE_DIFF(CURRENT_TIMESTAMP(), " \
                             f" DATE_ADD(date, INTERVAL credit_period DAY) , DAY) AS due_days FROM {dataset}.{CASH_FLOW_TABLE}"

            cashflow_list_data = cashflow_summary_obj.get_cash_values(get_cashflow_query)
            data = []
            for row in cashflow_list_data:
                data.append({'invoice_number':row[0], 'customer_name': row[1], 'credit_amount': row[2], 
                        'pending_amount': row[3], 'due_date': row[4], 'due_days': row[5]})
            info = {'status_code': '131', 'status': 'SUCCESS', 'message': "cash flow data list."}
            message = self.cash_flow_list(self, info=info, rows=data)
        except NotFound:
            info = {'status_code': '711', 'status': 'FAIL',
                'message': 'Unknown dataset, Please call Administrator...!'}
            data = {'total_receivables': 0, 'total_pending_receivables': 0, 'customers_overdue': 0}
            message = self.cash_flow_list(self, info=info, rows=data)
        return Response(data=message, status=status.HTTP_200_OK)

这是views.py文件,当前输出如下所示:

{
"info": {
    "status_code": "131",
    "status": "SUCCESS",
    "message": "cash flow data list."
},
"data": {
    "invoices": [
        {
            "invoice_number": 3,
            "customer_name": "Philip",
            "credit_amount": 25000,
            "pending_amount": 7760,
            "due_date": "2022-12-15T00:00:00Z",
            "due_days": 33
        },
        {
            "invoice_number": 1,
            "customer_name": "Charles",
            "credit_amount": 60000,
            "pending_amount": 45451,
            "due_date": "2022-12-31T00:00:00Z",
            "due_days": 17
        },
        {
            "invoice_number": 4,
            "customer_name": "John",
            "credit_amount": 60000,
            "pending_amount": 45451,
            "due_date": "2023-01-19T00:00:00Z",
            "due_days": -1
        },
        {
            "invoice_number": 5,
            "customer_name": "Jack",
            "credit_amount": 60000,
            "pending_amount": 50000,
            "due_date": "2023-01-23T00:00:00Z",
            "due_days": -5
        },
        {
            "invoice_number": 2,
            "customer_name": "Will",
            "credit_amount": 90000,
            "pending_amount": 89020,
            "due_date": "2023-01-01T00:00:00Z",
            "due_days": 16
        }
    ]
}

}
预期的结果是

{
"info": {
    "status_code": "131",
    "status": "SUCCESS",
    "message": "cash flow data list."
},
"data": {
    "count":5
    "previous":
    "next":
    "invoices": [
        {
            "invoice_number": 3,
            "customer_name": "Philip",
            "credit_amount": 25000,
            "pending_amount": 7760,
            "due_date": "2022-12-15T00:00:00Z",
            "due_days": 33
        },
        {
            "invoice_number": 1,
            "customer_name": "Charles",
            "credit_amount": 60000,
            "pending_amount": 45451,
            "due_date": "2022-12-31T00:00:00Z",
            "due_days": 17
        },
        {
            "invoice_number": 4,
            "customer_name": "John",
            "credit_amount": 60000,
            "pending_amount": 45451,
            "due_date": "2023-01-19T00:00:00Z",
            "due_days": -1
        },
        {
            "invoice_number": 5,
            "customer_name": "Jack",
            "credit_amount": 60000,
            "pending_amount": 50000,
            "due_date": "2023-01-23T00:00:00Z",
            "due_days": -5
        },
        {
            "invoice_number": 2,
            "customer_name": "Will",
            "credit_amount": 90000,
            "pending_amount": 89020,
            "due_date": "2023-01-01T00:00:00Z",
            "due_days": 16
        }
    ]
}

}
有什么理想的方法可以达到这样的产出吗?

sqxo8psd

sqxo8psd1#

我建议您继承"generics.GenericAPIView"类,并在"get"方法中示例化分页类。
大概是这样的

paginator = PageNumberPagination()
result_page = paginator.paginate_queryset(queryset, request)

然后,您可以按如下方式使用序列化程序类:

serializer = serializer(result_page, many=True)

如果您仍然不想使用序列化程序来获得正确的输出,请尝试解决这个问题。

相关问题