2017-12-20 5 views
0

블로그의 모든 게시물을 매년 보관하고 JSON을 내 앱에 전달하고 싶습니다.django rest framework : 매년 보관 파일을 만드는 방법

아래에서 뭔가를 찾고 있습니다.

[ 
    { 
    year:2017 
    url: 
    numbrofposts: 100 
    months: [ 
       { 
       month: Jan 
       numberofposts: 10 
       url: 
       listofposts: [ 
           { id:, title,url} 
           { id:, title,url} 
          ] 
       } 
      ] 
    } 
    { 
    year:2016 
    numbrofposts: 500 
    months: [ 
       { 
       month: Jan 
       numberofposts: 30 
       listofposts: [ 
           { id:, title, description, etc} 
           { id:, title, description, etc} 
          ] 
       } 
      ] 
    } 
... 
] 

가능하면 앞으로 게시물 수가 늘어날 것이므로 페이지 매김을 추가하는 방법. 이 가이드에서 http://www.django-rest-framework.org/api-guide/pagination/

: 나는

은 제목과 설명 그리고 내가 매김 가이드를 검토하는 것이 좋습니다

답변

1

CREATED_DATE으로, 내가 간단한 게시물 모델을 가정 매김이 년 또는 개월을 기반으로해야 확실하지 않다 :

페이지 뷰는 일반 뷰 또는 뷰 세트를 사용하는 경우에만 자동으로 수행됩니다. 일반 APIView를 사용하는 경우 페이지 매김 된 응답을 반환하도록 페이지 매김 API를 직접 호출해야합니다. 예를 들어 mixins.ListModelMixin 및 generics.GenericAPIView 클래스의 소스 코드를 참조하십시오.

0

현재 루프를 사용하여 아래의 JSON을 작성하고 있습니다.

article_dict2 = [] 
months_names = ['January','February','March','April','May','June','July','August','September','October','November','December'] 
for i in range(articles[0].publish_date.year, articles[len(articles)-1].publish_date.year-1, -1): 
    #number of articles per year 
    year_count = Article.objects.filter(publish_date__year=i).count() 
    if year_count != 0: 
     article_dict2_object = {} 
     article_dict2_object['year'] = i 
     article_dict2_object['total'] = year_count 
     article_dict2_object['months'] = [] 
     for month in range(13,1,-1): 
      #number of articles per month 
      count = Article.objects.filter(publish_date__year=i,publish_date__month=month).count() 
      if count != 0: 
       month_object = {} 
       month_object['mnum'] = month 
       month_object['name'] = months_names[month-1] 
       month_object['count'] = count 
       month_object['articles'] = PostSerializerCalendar(Article.objects.filter(publish_date__year=i,publish_date__month=month),many=True).data 
       article_dict2_object['months'].append(month_object) 
return Response(article_dict2) 

위의 질문은 효과적인 방법이며 더 좋은 방법입니다.

는 그리고 아래의 결과는

[ 
    { 
     "year": 2017, 
     "total": 6, 
     "months": [ 
      { 
       "mnum": 10, 
       "name": "October", 
       "count": 1, 
       "articles": [ 
        { 
         "id": 58, 
         "title": "I, Me and Mine", 
         "publish_date": "2017-10-14 18:04 UTC", 
        } 
       ] 
      }, 
      { 
       "mnum": 8, 
       "name": "August", 
       "count": 5, 
       "articles": [ 
        { 
         "id": 57, 
         "title": "Youth, Manhood and Old-age", 
         "publish_date": "2017-08-09 12:30 UTC", 
        }, 
        { 
         "id": 56, 
         "title": "Enjoy the Eternal, not the Interval", 
         "publish_date": "2017-08-08 15:20 UTC", 
        }, 
        { 
         "id": 55, 
         "title": "Setting a Bad Example", 
         "publish_date": "2017-08-05 12:30 UTC", 
        }, 
        { 
         "id": 54, 
         "title": "Living a Long Life", 
         "publish_date": "2017-08-04 12:30 UTC", 
        }, 
        { 
         "id": 53, 
         "title": "Dying in the Dark", 
         "publish_date": "2017-08-03 14:34 UTC", 
        } 
       ] 
      } 
     ] 
    } 
    ....... 
] 

내가 매김을 추가하는 방법을 잘 모르겠습니다 얻을 수있는에서 레벨 년 또는 월 또는 게시물. 내가 달 게시물하려고하면 어떻게하려고 다음 페이지

0

에서 계속을 표시하려면이 :

serializer.py

from rest_framework.pagination import PageNumberPagination 

class LargeResultsSetPagination(PageNumberPagination): 
    page_size = 10 #no of pages 
    page_size_query_param = 'page_size' 
    max_page_size = 10 

views.py

dataoforpost=models.objects.all() 
paginator = LargeResultsSetPagination() 
page = paginator.paginate_queryset(dataoforpost, request) 
response = paginator.get_paginated_response(page) 
return response