[英]How to add pagination to data received from google big query in django rest framework
我有一些从谷歌大查询收到的数据,我试图在 django rest 框架工作中对这些数据进行分页。 此外,我没有使用序列化程序来实现正确的 output。是否有任何理想的方法来添加我在底部提到的分页?
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文件,当前output如下:
{
"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
}
]
}
}
有什么理想的方法可以实现这个 output 吗?
我建议您继承“generics.GenericAPIView”class,并在您的“get”方法中实例化分页 class。
像这样:
paginator = PageNumberPagination()
result_page = paginator.paginate_queryset(queryset, request)
然后你可以像这样使用序列化程序 class:
serializer = serializer(result_page, many=True)
如果您仍然不想使用序列化程序来获得正确的 output,请尝试解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.