web2py - test for row with combination of two field values
I'm building a web2py controller in which I need to query a table for a combination of value x in one field AND value y in a second field (in the same row). To query on a single field, I would just write
db.table.field == x
But I don't know how to write a query that looks for fiel开发者_StackOverflowd==x AND field2==y
(db.table.field1==x)&(db.table.field2==y)
See the book section on logical operators.
For a more advanced version, you can append a query to a list and use python's reduce function.
queries=[]
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)
db(query).select()
精彩评论