Jquery – Any way to serialize a pagination object in Django


I'm using Django pagination with jQuery. I can serialize the objects list of the pagination object, but I'd like to serialize the whole object to get more data (page number, total number of pages…).
How can I serialize the whole pagination object?



function getRestaurants(query) {

        $.post("/getRestaurant/", query,
            function(data) {
                /* do stuff with data */
             },"json" );


def getRestaurant(request):

    results = Restaurant.objects.all()
    paginator = Paginator(restaurants, 5)

    # Make sure page request is an int. If not, deliver first page.
        page = int(request.POST.get('page','1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
        results = paginator.page(page)
    except (EmptyPage, InvalidPage):
        results = paginator.page(paginator.num_pages)

    data=serializers.serialize("json", results.object_list) #I'd like to serialize the whole results object

    return HttpResponse(data)

Best Solution

I've just had to make a hacky solution for this so I'll post it as an answer - any improvements welcome:

from django.core import serializers
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.utils import simplejson  
from types import MethodType 

from mysite.tasks.models import Task


def list(request):
    Return a paginated JSON object.

    paginator = Paginator(tasks.objects.all(), PER_PAGE) 
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
        pagetasks = paginator.page(page)
    except (EmptyPage, InvalidPage):
        pagetasks = paginator.page(paginator.num_pages)

    # Dump the Page attributes we want to a dictionary
    serializedpage = {}
    wanted = ("end_index", "has_next", "has_other_pages", "has_previous",
            "next_page_number", "number", "start_index", "previous_page_number")
    for attr in wanted:
        v = getattr(tasks, attr)
        if isinstance(v, MethodType):
            serializedpage[attr] = v()
        elif isinstance(v, (str, int)):
            serializedpage[attr] = v

    # Serialise the queryset to plain Python objects 
    # and add them to the serialized page dictionary
    pythonserializer = serializers.get_serializer("python")()
    serializedpage["object_list"] = pythonserializer.serialize(pagetasks.object_list, 
            fields=('task_id', 'task_data')) 

    # Dump it as JSON using the Django encoder
    response = HttpResponse(mimetype="application/json")
    simplejson.dump(serializedpage, response, cls=DjangoJSONEncoder)
    return response