朋友,
在一些python脚本之后,我使用POST方法在Django中得到以下响应:
[{'ID': 76544,
'Location': 'NM, USA',
'Location_score': 0.7142857142857143,
'No_of_matched_comp': 3,
'No_of_matched_industry': 3,
'over_all_score': 91,
'title': 'Accounting',
'title_score': 1.0},
{'ID': 76545,
'Location': 'NM, USA',
'Location_score': 0.7142857142857143,
'No_of_matched_comp': 3,
'No_of_matched_industry': 3,
'over_all_score': 91,
'title': 'Accounting',
'title_score': 1.0},
{'ID': 55557,
'Location': 'CO, USA',
'Location_score': 0.7142857142857143,
'No_of_matched_comp': 2,
'No_of_matched_industry': 2,
'over_all_score': 74,
'title': 'Account',
'title_score': 0.8235294117647058}]
计数是50所以我想从Response每页10个数据。现在我想分页这个Response。 我的输入参数与输出参数不同。
输入参数model.py如下:
class TalentSearchInput(models.Model):
Date = models.CharField(max_length = 100,blank=True, null=True)
Title = models.CharField(max_length = 100)
Location = models.CharField(max_length = 100)
Competency_user = models.CharField(max_length = 100,blank=True, null=True)
Comp_ID = ArrayField(ArrayField(models.IntegerField()))
Day_rate_lower = models.IntegerField(blank=True, null=True)
Day_rate_upper = models.IntegerField(blank=True, null=True)
hourly_rate_lower = models.IntegerField(blank=True, null=True)
hourly_rate_upper = models.IntegerField(blank=True, null=True)
Industry_ID = ArrayField(ArrayField(models.IntegerField()),blank = True, null = True)
def __str__(self):
return self.title
如何在APIView中的Django REST API中进行分页?
我经历了以下链接:
http://www.django-rest-framework.org/api-guide/pagination/#limitoffsetpagination
https://docs.djangoproject.com/en/dev/topics/pagination/
Django Rest Framework 3.1 breaks pagination.PaginationSerializer
我的View.Py文件如下:
from django.shortcuts import render
# Create your views here.
import json
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import TalentSearchInput, EmployerInput, SearchFilter#, TalentScoring
from .serializers import TalentSearchSerializer, EmployerSerializer, FilterSerializer,TalentScoringSerializer
#from rest_framework.pagination import PageNumberPagination
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
#from django.shortcuts import render
#from base_searchAPP.pagination import CustomPagination
#from rest_framework.settings import api_settings
#from base_searchAPP.mixin import MyPaginationMixin
#Python Code Modules
import pandas as pd
import numpy as np
import MySQLdb
import MySQLdb.cursors
import json
import os
from difflib import SequenceMatcher as SM
from nltk.util import ngrams
import codecs
from collections import defaultdict
# Talent Search Views
@api_view(['GET', 'POST'])
#@permission_classes([AllowAny,])
def TalentInputparameters_list(request):
"""
List all code snippets, or create a new snippet.
"""
if request.method == 'GET':
print('GET.call')
#print(Snippet)
TalentSearch = TalentSearchInput.objects.all()
serializer = TalentSearchSerializer(TalentSearch, many=True)
print(serializer)
d = Response(serializer.data)
return d
elif request.method == 'POST':
print('POST.call')
# Input Data
d = request.data
print(d)
#Input Python code Here
# Load Json into python
#d = json.loads(d)
这是我使用此方法的文件的一部分。
我尝试按照全局分页的第一个链接建议在setting.py文件中插入以下行
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE' : 10
}
请告诉我我的输出参数是不同的,所以我应该为输出参数制作模型和序列化器吗?
如果是,那么
当我在view.py文件中添加模型和序列化程序类时,我应该在哪里添加我的 响应所以它会分页?
非常感谢你。
如果我的问题不正确,请原谅我,如果有人需要更多信息,请告诉我
答案 0 :(得分:0)
你可以使用Django Paginator。 For More
for (Message message : messages) {
message.setFlag(Flags.Flag.SEEN,true);
}
希望它会有所帮助。
答案 1 :(得分:0)
你可以试试这个:
from django.core.paginator import Paginator
size = 10
paginator = Paginator(TalentSearch, size)
resources = paginator.page(page)
serializer = TalentSearchSerializer(resources, many=True)
return Response(serializer.data)
其他例子也许可以在这里找到
答案 2 :(得分:0)
使用类基本视图,你可以这样做
from rest_framework.pagination import PageNumberPagination
from rest_framework import generics
class TalentSearchpagination(PageNumberPagination):
page_size = 10
class BlogListing(generics.ListAPIView):
pagination_class = TalentSearchpagination
serializer_class = TalentSearchSerializer
def get_queryset(self):
TalentSearch = TalentSearchInput.objects.all()
return TalentSearch
答案 3 :(得分:0)
由于您使用的是FBV,因此您需要像这样调用分页类,
from rest_framework.pagination import LimitOffsetPagination
def TalentInputparameters_list(request):
"""
List all code snippets, or create a new snippet.
"""
if request.method == 'GET':
# print(Snippet)
talent_search_queryset = TalentSearchInput.objects.all()
page = LimitOffsetPagination()
paginated_result = page.paginate_queryset(talent_search_queryset, request)
serializer = TalentSearchSerializer(paginated_result, many=True)
return Response(serializer.data)
你的api url就像,/host/end/point/?limit=10&offset=0
答案 4 :(得分:0)
非常感谢您的回答。但它通过以下代码解决:
我在model.py文件中为输出参数创建了Model:
class TalentScoring(models.Model):
ID = models.IntegerField(blank=True, null=True)
title = models.CharField(max_length = 100)
Location = models.CharField(max_length = 100)
Location_score = models.IntegerField(blank=True, null=True)
No_of_matched_comp = models.IntegerField(blank=True, null=True)
No_of_matched_industry = models.IntegerField(blank=True, null=True)
over_all_score = models.IntegerField(blank=True, null=True)
title_score = models.IntegerField(blank=True, null=True)
我已经完成了此模型的序列化程序,如下所示,它将序列化我的响应并提供输出 我的serializer.py文件很糟糕:
class TalentScoringSerializer(serializers.ModelSerializer):
class Meta:
model = TalentScoring
fields = ('ID','title','Location','Location_score','No_of_matched_comp','No_of_matched_industry','over_all_score','title_score')
view.py文件如下
from rest_framework.pagination import PageNumberPagination
paginator = PageNumberPagination()
paginator.page_size = 10
result_page = paginator.paginate_queryset(final_Score, request)
serializer = TalentScoringSerializer(result_page, many=True)
return paginator.get_paginated_response(serializer.data)
我的列表名称是" final_Score"在输入参数上完成一些python编码之后 现在我可以按照我的愿望每页显示10个数据。