friends,
After some python scripting I get below response in Django using POST method:
[{'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}]
Count is 50 so I want to 10 data per page from Response. Now I want to paginate this Response. my input parameters Different from Output parameters.
input parameters model.py is below:
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
How to do pagination in Django REST API in APIView?
I gone through following links:
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
My View.Py file as below:
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)
This is part of file I am using this method.
I tried to insert following lines in setting.py file as per first link suggestion for Globally pagination
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE' : 10
}
please advice me my Output parameters are different so should I make Model and Serializers for output parameters?
if yes then
when I add model and serializer class in view.py file where should I add my Response so it will paginate?
Thank you so much.
If my Question is not properly please forgive me and let me know if anyone need more information