python - jquery datatables with mongodb(mongoengine)

techno206 at techno206 at
Wed Dec 18 13:20:07 CET 2013

I am new to Python, MongoDB(mongoengine(ODM)) and the pyramid framework as a whole. I am currently working on a project using the above mentioned technologies and I want to use jQuery datatables(also new to this) I found a link on their site on how to use datatables with MongoDB, but it is in php and my translation skills aren't that good.

my question is:
Is it possible to use datatables using the above mentioned technologies, if yes, then how?

I have tried converting someone else's code that used SQLAlchemy with datatables, but I am stuck as I do not know how to change the function.


def candidate_list_json(context, request):
    def format_output(vals):
        vals = list(vals)
        vals[-1] = """<div class="btn-group"><a href='%s' class=btn>View</a><a href='%s' class=btn>Edit</a></div>""" % (
            request.route_url('candidates', id_number=vals[-1], traverse=()),
            request.route_url('candidates', id_number=vals[-1], traverse="edit")
        vals[0] = "<a href='%s'>%s</a>" % (request.route_url('candidates', id_number=vals[0], traverse=()), vals[0])
        return vals
    if has_permission('admin', context, request):
        basefilt = None                  # I changed Up to here
    return handle_datatable(
        [Agents.db_agent_id, Agents.db_name, Agents.id_number, Agents.mobile_number,, Agents.db_agent_id],
        lambda term: or_('%'+term+'%'),'%'+term+'%'),'%'+term+'%'),'%'+term+'%'), ),

handle_datatable is a method:

def handle_datatable(request, idcol, cols, filtfunc, options=None, cache='short_term', formatfunc=None, displaylength=90, join=None, base_filt=None, outerjoin=None, groupby=None, no_paginate=False, ordercols=None, orderby=None, printquery=False, nocount=False):
s = sqlahelper.get_session()
if groupby is not None and type(groupby) != list and type(groupby) != tuple:
    groupby = [groupby]

def attachfilt(q, filt, nogroup=False):
    if filt:
        q = q.filter(and_(*filt))
    if join:
        q = q.join(*join)
    if outerjoin:
        q = q.outerjoin(*outerjoin)
    if options:
        q = q.options(*options)
    if groupby and not nogroup:
        for g in groupby:
            q = q.group_by(g)
    return q

def perform_count(filt, idcol, term):
    if not nocount:
        return attachfilt(s.query(idcol), filt, nogroup=False).count()
        return 0

#@cache_region('short_term', 'handle_search')
def perform_search(filt, cols, iStart, iLength, order_cols):
    q = attachfilt(s.query(*cols), filt)
    if order_cols:
        q = q.order_by(*order_cols)

    if printquery:
        print q
    if no_paginate:
        rows = q.all()
        rows = q[iStart:iStart+iLength]
    if callable(formatfunc):
        data = [formatfunc(row[:]) for row in rows]
        data = [row[:] for row in rows]
    return data

if not callable(filtfunc):
    raise Exception("Filter Function is not callable")
if not cols:
    raise Exception("Please provide columns to search")

if not no_paginate:
    iStart = int(str(request.params.get("iDisplayStart", 0)))
    iLength = int(str(request.params.get("iDisplayLength", displaylength)))
    iStart = 0
    iLength = 0

if not ordercols:
    ordercols = cols

if orderby:
    order_cols = orderby
    order_cols = []
if request.params.get("iSortCol_0", None):
    iSortingCols = int(str(request.params.get('iSortingCols', 0)))
    for k in range(0, iSortingCols):
        iSortCol = int(str(request.params.get('iSortCol_%s' % k, 0)))
        sSortDir = str(request.params.get('sSortDir_%s' % k, 0))
        if str(request.params.get('bSortable_%s' % iSortCol, 'false') == 'true'):
            col = ordercols[iSortCol]
            if sSortDir == "asc":
search = request.params.get("sSearch", None)
filt = []
if search:
    filt = filtfunc(search)
if filt is not None and type(filt) != list:
    filt = [filt]
if type(cols) != list:
    cols = [cols]

itotal = perform_count([base_filt], idcol, search)
if no_paginate:
    iLength = itotal
if base_filt is not None:
cnt = perform_count(filt, idcol, search)
data = perform_search(filt, cols, iStart, iLength, order_cols)

return dict(
    sEcho=request.params.get("sEcho", 0),

As I said before I am new to these Technologies, but I am willing to learn, if you can just point me in the right direction.

More information about the Python-list mailing list