'how to get json object out of django paginator value

am trying to get a Json value out of Django Paginator object so that i can return it back to html file as a json response via JsonResponse method.Please help Heres my Code

users=User.objects.all().order_by("id")
book_paginator=Paginator(users,10)
page_num=request.GET.get('page') // say page==2
page=book_paginator.get_page(page_num)
retutn JsonResponse({'data':page)

I know it wont work but you get the point of what am trying to archieve. Please help Thank you.



Solution 1:[1]

you can iterate the get_page() result

from django.forms.models import model_to_dict

pages = book_paginator.get_page(page_num)
data = [model_to_dict(page) for page in pages]
return JsonResponse({'data': data)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 emaniacs