common_filter

41 views
Skip to first unread message

Paolo Amboni

unread,
Jul 3, 2015, 12:00:22 PM7/3/15
to web...@googlegroups.com
Can someone translate the expression

db.blog_post._common_filter = lambda query: db.blog_post.is_public == True

without the use of lambda function?

I'm trying to understand how to set up common filter in controller but lambda function always confuse me.

So, for now, the only way to set the filter is to copy the code without understanding it!!

黄祥

unread,
Jul 3, 2015, 1:47:26 PM7/3/15
to web...@googlegroups.com

Anthony

unread,
Jul 3, 2015, 5:38:00 PM7/3/15
to web...@googlegroups.com, amb...@gmail.com
A lambda function is just an anonymous function (i.e., one you define in place without naming it). The common_filter does not have to be a lambda function -- it can be any callable object that takes a single argument and returns a Query object. So, you could do:

def my_common_filter(query):
   
return db.blog_post.is_public == True
db
.blog_post._common_filter = my_common_filter

When the common filter function is called, the current query will be passed to it. In the above example, that query argument is ignored, but in theory, you could inspect that query argument and have the return value depend on it somehow. The final query will end up being a conjunction of the original query and the return value of the common filter function.

Also, note that the argument of the common filter function (whether a lambda function or otherwise) does not have to be "query" -- that is just a convention to remind you that the original query will be passed into the function. You can name that argument whatever you want.

Anthony

Paolo Amboni

unread,
Jul 6, 2015, 11:22:10 AM7/6/15
to web...@googlegroups.com, amb...@gmail.com
Now it makes much more sense for me, i'm coming from PHP programming!!!
Thanks.
Reply all
Reply to author
Forward
0 new messages