Hi Cristina,

 

Can you show us your view code as well?

 

Assuming you are trying to use datatables instead of web2py Grid or Smartgrid 
(which gives you ability to build dynamic queries), you will probably need to 
loop through all the fields in the table (server side) and build your query 
based on the value entered in the serach box.

 

Referenced fields will generally be displayed as the id of the referencing 
field depending on how you pass the information back to your view (i.e rendered 
or not) so you will only be able to search on the value in the view.

 

Alternatively you can look at the individual field.type (which will tell you 
the type of field you are dealing with) and treat each field type accordingly.

 

Hope this helps

John

 

 

From: web2py@googlegroups.com [mailto:web2py@googlegroups.com] On Behalf Of 
Cristina Sig
Sent: 17 June 2019 03:36
To: web2py-users
Subject: [web2py] web2py dynamic queries

 

Hello,

 

I'm a newbie on python language so I'm struggling to do some queries.

I have a table Student which I use to fill a table on view, above that table, I 
have a search box, where I would like to provide the option to search in 
multiple columns (phone, last name, first name, or nationality).

I don't know how exactly do a dynamic query in this case because I have some 
fields which are references from other tables.

 

This is my Db

db.define_table('Nationality',
                Field('descripcion', 'string'),
               )

db.define_table('Grade',
                Field('level', 'string'),
               )

db.define_table('Student',
                Field('lastname', 'string'),
                Field('firstname', 'string'),
                Field('nationality','reference Nationality'),
                Field('phone', 'integer'),
                Field('email', 'string'),
                Field('gradelevel','reference Grade'),
               )

 

and this is my try so far

queries = [(db.Student.id > 0)]
    if search_value and search_value != '' and search_value != 0:
        queries.append(db.Student(search_value))  
        query = reduce(lambda a,b:(a&b),queries) 

        query.select()

 

 

I found a generic way to do it but still don't know how to deal with references.
Any suggestion/idea would be very appreciated :)

 

queries=[]
if arg1 == "xyz": queries.append(db.abc.id > 0)
if arg2 == "xyz": queries.append(db.def.id > 0)
query = reduce(lambda a,b:(a&b),queries)
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/54dc8ca8-bebe-4604-8e4e-a89c254ed944%40googlegroups.com
 
<https://groups.google.com/d/msgid/web2py/54dc8ca8-bebe-4604-8e4e-a89c254ed944%40googlegroups.com?utm_medium=email&utm_source=footer>
 .
For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/050901d524e4%24075852f0%241608f8d0%24%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to