Building a query

31 views
Skip to first unread message

Gaël Princivalle

unread,
Apr 4, 2020, 10:36:13 AM4/4/20
to web2py-users
Hello.

How can I build a query in 2 steps?

query = db.products.type == 1
query = query and db.products.stock > 0

The final result must do something like that:
fruits = db((db.products.type == 1) and (db.products.stock > 0)).select()
Made like this:
fruits = db(query).select()

Thanks!

Val K

unread,
Apr 4, 2020, 10:57:40 AM4/4/20
to web2py-users
query &= ...
Just use & and | instead of 'and' and 'or'

Thiago Gomes

unread,
Apr 4, 2020, 11:06:36 AM4/4/20
to web...@googlegroups.com
query = (db.products.type == 1)
query = query & (db.products.stock > 0)

fruits = db((db.products.type == 1) & (db.products.stock > 0)).select()

fruits = db(query).select()


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/4c069202-a423-458e-9305-e22322d32f72%40googlegroups.com.

Gaël Princivalle

unread,
Apr 5, 2020, 5:03:56 AM4/5/20
to web2py-users
Thanks a lot Val and Thiago for your help, it's perfect.
Reply all
Reply to author
Forward
0 new messages