How to add pagination to data received from google big query in django rest framework

I have some data that is received from google big query and I'm trying to pagination to this data in django rest frame work. Also I'm not using serializers to achieve the correct output. Is there any ideal way to add pagination as I mention at the bottom?

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)

This is the views.py file and the current output is as folows:

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

}

And the expected result is

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

}

Is there any ideal way to achieve this output?

I would suggest you inherit the "generics.GenericAPIView" class and in your "get" method you should instantiate the pagination class.

Something like this:
paginator = PageNumberPagination()
result_page = paginator.paginate_queryset(queryset, request)

And then you can use the serializer class as so:
serializer = serializer(result_page, many=True)

If you still don't want to use serializers to achieve the correct output, try and work around this.

Back to Top