web2py belongs if none

29 views
Skip to first unread message

Yebach

unread,
May 30, 2019, 9:41:52 AM5/30/19
to web2py-users
Hello

I have a query where the possibility for belongs can be a set of values or it can be empty. 
In case the set is empty I want the query to act as 1=1 and not 1=0, so all values would be selected.

Any suggestions??

Thank you

This is my query

selected = list(selected_values_str.split(","))
            selected_int  = tuple([int(i) for i in selected])
            rows = db((db.counters.c_date >= datum_od) &
                (db.counters.c_user_inserted.belongs(selected_int)) &
                (db.counters.c_date <= datum_do) &
                (db.machines.m_status != 100)).select(#db.counters.c_machine_id,
                                                      db.counters.c_total,
                                                      db.counters.c_black,
                                                      db.counters.c_color,
                                                      db.counters.c_level1,
                                                      db.counters.c_level2,
                                                      db.counters.c_level3,
                                                      db.counters.c_a3_color,
                                                      db.counters.c_date,
                                                      db.counters.c_datetime,
                                                      db.counters.c_datetime_dataread,
                                                      db.counters.c_scans,
                                                      db.counters.c_note,
                                                       db.machines.m_code,
                                                       db.auth_user.first_name,
                                                       db.auth_user.last_name,
                                                       db.auth_user.email,
                                                       left=[db.machines.on(db.counters.c_machine_id == db.machines.id),
                                                             db.auth_user.on(db.counters.c_user_inserted== db.auth_user.id)]
                                                      ).as_list()

Val K

unread,
May 30, 2019, 4:14:18 PM5/30/19
to web2py-users

why not just:
# start to form query:
q
=
db.counters.c_date >= datum_od
if ...: # continue to form the query depending on conditions  
   q
&=...belongs(...)

# the same with `left`
left
= ...
if ...:
   left
.append(...)

# and fields
if ...:
   fields
= [...]
else:
   fields
= [...]

# at the end
rows
= db(q).select(*fields, left = left)


Reply all
Reply to author
Forward
0 new messages