[web2py] Re: Selecting posts with multiple categories

2014-07-20 Thread Ruud Schroen
I went for the virtual field approach. My field in my model: #Virtual field for showing categories in view def categories(row): links = db(db.post_to_cat.post_id == row.blog_post.id).select() ids = [{'name':l.cat_id.name, 'slug':l.cat_id.slug} for l in links] return ids #Appending the

[web2py] Re: Selecting posts with multiple categories

2014-07-20 Thread Massimo Di Pierro
Good point. There are various ways 1) add a virtual fields fetches them for each row 2) add a virtual field that fetches and cached per each request 3) denormalize the database and add a Field('categories','list:string categories') and you keep in sync with the link table. On Saturday, 19 July 2

[web2py] Re: Selecting posts with multiple categories

2014-07-19 Thread Ruud Schroen
Thanks for the info Massimo. I made a link table like you said. Something like this is ok? db.define_table('post_to_cat', Field('post_id', 'reference blog_post'), Field('cat_id', 'reference blog_cat') ) Querying should be easy, there's just one que

[web2py] Re: Selecting posts with multiple categories

2014-07-12 Thread Massimo Di Pierro
Do not do dal queries in a try..except. If the db query fails it may not be automatically rolled back. I think you want: category = request.args(0) and db.blog_cat(slug=request.args(0)) query = db.blog_post.blog_cat.contains(category.id) if category else db.plog_post posts = db(query).select(ord