Django - Возврат постраничного набора объектов в виде JSON-ответа
Я использую Django 3.2
Я хочу вернуть постраничный набор записей в формате JSON. Вот мой фрагмент кода:
class GetCommentChildren(SocialAppUserMixin, View):
def post(self, request, *args, **kwargs):
comment_id = self.request.POST['pid']
pagenum = self.request.POST.get('page', 1)
comments_per_page = self.request.POST.get('per_page', 10)
retval = False
comments = []
try:
comment = Comment.objects.get(id=comment_id)
children = comment.get_children()
paginator = Paginator(children, comments_per_page)
try:
comments = paginator.page(pagenum)
except PageNotAnInteger:
comments = paginator.page(1)
except EmptyPage:
comments = paginator.page(paginator.num_pages)
except ObjectDoesNotExist:
logger.exception(f"Caught ObjectDoesNotExist exception in GetCommentsChildren::post(). Comment id: {comment_id}")
except Exception as e:
logger.exception(f"Caught Exception in GetCommentsChildren::post(). Details: {e}")
return JsonResponse({'ok': retval, 'comments': comments})
Как вернуть постраничный набор записей в формате JSON?