If you make sure your URL vars have the same names as the associated table
fields, you can simplify further as follows:
def eco:
my_content = db.table.created_by == auth.user.id
filter_query = reduce(lambda a, b: a & b,
[db.table[v] == request.vars[v] for v in request.
vars
if v in db.table.fields])
filtered_list = db(my_content & filter_query).select(,,,,,)
return dict(filtered_list = filtered_list)
or if you prefer loops:
def eco:
filter_query = db.table.created_by == auth.user.id
for var in [v for v in request.vars if v in table.fields]:
filter_query &= db.table[var] == request.vars[var]
filtered_list = db(my_content & filter_query).select(,,,,,)
return dict(filtered_list = filtered_list)
Anthony
On Saturday, February 22, 2014 3:25:34 PM UTC-5, Avi A wrote:
>
> Great, thanks. the "moreover" idea is what i was missing. Yeah, I'll
> replace the args with vars url, thanks.
>
>
> On Saturday, February 22, 2014 9:36:58 PM UTC+2, Niphlod wrote:
>>
>> every
>> db.table.column == parameter
>> is something that further reduces the resultset (it's a "filter"
>> condition)
>> in case "parameter" is 0, just don't use that condition in the query ^_^
>>
>> moreover, you can do
>>
>> filter_condition = (db.table.created_by == auth.user_id)
>> if .....
>> filter_condition = filter_condition & (db.table.field_a == something)
>> ....
>> ....
>>
>> filtered_set = db(filter_condition).select()
>>
>>
>> PS: usually filters are more "manageable" with request.vars than
>> request.args() ... it's a little unconfortable to remember the order of the
>> filter and to specify 0 as the "ALL" parameter is counterintuitive ....
>> eco/0/1/0/6/0/0 sounds a lot more ugly than eco?b=1&d=6
>>
>> On Saturday, February 22, 2014 7:28:33 PM UTC+1, Avi A wrote:
>>>
>>> Hi,
>>> Assuming I want to create an ajax query that will filter the results
>>> according to some data in fields:
>>> for example: ajax url: ... eco/1/6/4/5/3
>>>
>>> def eco:
>>> my_content = db.table.created_by == auth.user.id
>>> query_a = db.table.field_a == request.args(0)
>>> query_b = db.table.field_b== request.args(1)
>>> query_c = db.table.field_c == request.args(2)
>>> query_d = db.table.field_d == request.args(3)
>>> query_e = db.table.field_e == request.args(4)
>>>
>>> filtered_list = db((my_content) & (query_a) & (query_b) & (query_c)
>>> & (query_d) & (query_e)).select(,,,,,)
>>> return dict(filtered_list = filtered_list)
>>> I guess, so far so good.
>>>
>>> Now if any arg == 0 , (for example: eco/2/0/0/0/3) I want it to skip
>>> that specific query.
>>>
>>> So I want to a chive something like that:
>>> if request.args(0) != 0:
>>> query_a = db.table.field_a == request.args(0)
>>> else:
>>> query_a = db.table.field_a == ALL
>>> if request.args(01) != 0:
>>> etc"...
>>>
>>> with what should I replace the ALL? or the else part?
>>>
>>> Thanks.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.