Published in 21:16 of 08/10/2012 by

Published in 21:16 of 08/10/2012

←Home

web2py dynamic queries with reduce

Sometimes, specially when creating search engines, we may need to build the queries dynamically. web2py’s DAL is flexible and permits you to use Python reduce for this job.

queries=[db.table]
if arg1 == "x":   
    queries.append(db.table.field == x)
if arg2 == "y": 
    queries.append(db.table.otherfield == y)
# many conditions here....
query = reduce(lambda a, b:(a & b), queries)
rows = db(query).select()
  • O Natal Animal 2013 está no ar in Home · 23:24 of 12/10/2013
  • Desenvolvendo protótipos para startups com Python e web2py in web2py · 20:25 of 11/19/2013
  • Gravando logs de aplicativos web2py in web2py · 00:29 of 01/21/2013

  • comments powered by Disqus Go Top