过滤Web2py smartgrid链接表上的记录

时间:2014-03-12 00:06:15

标签: web2py

我发现web2py Smartgrid表单功能非常强大,但我找不到如何在链接表上过滤行,例如在查询或类似内容中。

这是返回要显示的行(paks)的函数:

def opened_paks():
    opened = (db.pak.id==db.mov_pak.pak) & (db.mov_pak.event=='Open')
    records = SQLTABLE(db(opened).select(db.pak.ALL))
    return dict(records=records)

我需要限制链接表(item_order)的网格,一旦选中, ADD 只有来自opens_paks()的行,smartgrid定义:

def manage_orders():
    #here is where is needed to define a constraint to limit
    #on item_order form to just pak row from opened_paks()
    grid = SQLFORM.smartgrid(db.order,linked_tables=['item_order'],
                         user_signature=False
                         )
    return dict(grid=grid)

当然item_order与其他模型有关:

db.define_table('order',
    Field('cliente', 'reference cliente'),
    Field('code','integer'),
    Field('fecha','date',label="Fecha",default=request.now,notnull=True),
    migrate='orden.table',
    format=lambda r: '%s->%s' % (r.code, db.cliente[r.cliente].name ) 
)

db.define_table('pak',
    Field('producto','reference producto'),
    Field('qty','double'),
    migrate='pak.table',
    format=lambda r: '%s->%s' % (r.id, db.producto[r.producto].descripcion )
    )

db.define_table('item_orden',
    Field('order', 'reference order'),
    Field('pak', 'reference pak'),
    Field('qty','double',notnull=True),
    Field('precio_unitario','double',label="Precio Unitario",notnull=True),
    migrate='item_order.table',
    format = '%(orden)s %(paquete)s %(id)s'
    )

db.define_table('mov_pak',
    Field('paquete','reference pak'),
    Field('fecha','date',label="Fecha",notnull=True),
    Field('event',label="Evento",requires=IS_IN_SET(['I','M','Open', 'C'])),
    migrate='mov_pak.table',
    format = '%(paquete)s %(fecha)s %(evento)s'
    )

1 个答案:

答案 0 :(得分:0)

在Web2py西班牙语小组的帮助下,实现此目的的方法是使用common_filters,如下一种可能的方式,他们无论如何都要使用常见的过滤器:

def manage_orders():
    opened = (db.pak.id==db.mov_pak.paquete) & (db.mov_pak.evento=='Open')
    opened_rows = db(opened).select()
    opened_set= set()
    for r in opened_rows:
        opened_set.add(r.pak.id)
    # apply filter, since opened query returns a tuple and filter expects table.id 
    #previous lines were requiered
    db.pak._common_filter = lambda query: db.pak.id.belongs(opened_set)
    grid = SQLFORM.smartgrid(db.orden,linked_tables=['item_order'],
                         user_signature=False, csv=False,
                         )
    return dict(grid=grid)