DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Flexigrid Django Utility Class

  • submit to reddit
        // Utility class for working with jQuery Flexigrid data grid plugin from with Django

import simplejson
from django.core.paginator import Paginator

class Flexigrid:
    Utility class for interacting with Flexigrid
        This snippet is (c) 2009 Jason McVetta <>, and
        is released under the GPL.  
    def __init__(self, request):
        Extracts standard Flexigrid variables from a Django REQUEST object.
        log.debug('request.POST: %s' % request.POST)
        self.sortname = request.REQUEST.get('sortname', 'id') # Field to sort on = request.REQUEST.get('page', 1) # What page we are on
        self.sortorder = request.REQUEST.get('sortorder', 'asc') # Ascending/descending
        self.rp = int(request.REQUEST.get('rp', settings.CASES_PER_PAGE)) # Num requests per page
        self.qtype = request.REQUEST.get('qtype', None) # Query type
        self.query = request.REQUEST.get('query', None) # Query string
        log.debug('sortname: %s' % self.sortname)
        log.debug('page: %s' %
        log.debug('sortorder: %s' % self.sortorder)
        log.debug('rp: %s' % self.rp)
        log.debug('qtype: %s' % self.qtype)
        log.debug('query: %s' % self.query)
    def json(self, rows, use_paginator=True):
        Returns JSON suitable for feeding to Flexigrid.
        @type rows: [{'id': 1, 'cell': ['field1, 'field2', ...]}, ...]
        @param use_paginator: Set this to false if you are going to do 
            pagination outside this class, for instance if you have a very
            large set of objects and do not want to fetch all rows.
        p = Paginator(rows, self.rp)
        if use_paginator:
            rows =
        json_dict = {
            'total': p.count,
            'rows': rows
        json = simplejson.dumps(json_dict)
        return json


Snippets Manager replied on Wed, 2009/12/30 - 7:31pm

Any change you can shed a little more light on how this will work? I am using django and would like to use flexigrid.