Python django rest框架:如何创建年度归档
我想为我博客中的所有帖子创建一个年度存档,并将其作为JSON传递给我的应用程序 我在下面找东西Python django rest框架:如何创建年度归档,python,django,django-rest-framework,Python,Django,Django Rest Framework,我想为我博客中的所有帖子创建一个年度存档,并将其作为JSON传递给我的应用程序 我在下面找东西 [ { year:2017 url: numbrofposts: 100 months: [ { month: Jan numberofposts: 10 url: listofposts: [
[
{
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}
]
}
]
}
...
]
如果可能的话,如何添加分页,因为将来的职位数量将不断增加。我不确定分页是基于年还是基于月
假设我有一个简单的Posts模型,带有标题、描述和创建日期,我建议查看分页指南: 根据该指南: 仅当使用常规视图或视图集时,才会自动执行分页。如果您使用的是常规APIView,则需要自己调用分页API,以确保返回分页响应。有关示例,请参阅mixins.ListModelMixin和generics.GenericAPIView类的源代码
我目前正在使用for循环创建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",
}
]
}
]
}
.......
]
我不知道如何添加页码从哪个级别的年或月或职位。如果我尝试了一个月,或者发布了如何在下一页显示续页的帖子,请尝试以下方法:
序列化程序.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
dataoforpost=models.objects.all()
paginator = LargeResultsSetPagination()
page = paginator.paginate_queryset(dataoforpost, request)
response = paginator.get_paginated_response(page)
return response
视图.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
dataoforpost=models.objects.all()
paginator = LargeResultsSetPagination()
page = paginator.paginate_queryset(dataoforpost, request)
response = paginator.get_paginated_response(page)
return response