Web2py - test for a string with a combination of two field values

I am creating a web2py controller in which I need to query a table for a combination of the value of x in one field AND the value of y in the second field (on the same line). To request in one field I will simply write

db.table.field == x 

But I don't know how to write a query that looks for field==x AND field2==y

+4
source share
2 answers
 (db.table.field1==x)&(db.table.field2==y) 

See the logical operator book section.

+5
source

For a more advanced version, you can add the query to the list and use the python shortcut 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() 
+1
source

Source: https://habr.com/ru/post/1369593/


All Articles