Hm.. Maybe I'm not understanding something. I thought the standard way of creating a many to many relationship was to create a table which holds the ids of the two things you're creating a relationship with. With this example,
db.define_table('products', Field('name')) db.define_table('thefilter', Field('name')) db.define_table('thefilter_products', Field('product', db.products), Field('thefilter', db.thefilter)) Then, when a product has a new filter called "Tops" and "Sweatshirts" added to it, that relationship would be stored in the 'thefilter_products" table. Essentially, I'm building a tag cloud. A product is represented by many things (filters). I used a many-to-many because a product can have many of these filters, and a filter can be applied to multiple products. The solution you posted was neat, but I don't think it'll work in this case? That would work if only the filter "Tops" was used, but what if I want a product which has at least "Tops" and "Sweatshirts"? So a product with only "Tops" would not be included because "Sweatshirts" wasn't part of it. To further complicate things, what if the number of combinations of filters is defined by the user? For example, a user could specify "Tops" or "Tops", "Sweatshirts" or "Tops", "Sweaters", "Fluffy", etc. This may mean that the query may have to be dynamically built? I may end up just accepting any product which has any of the filters specified because this seems a bit complicated. Maybe it's not correctly designed? On Friday, May 10, 2013 11:44:02 AM UTC-4, Niphlod wrote: > > assuming that your filter is indeed > > myfilter = db(db.thefilter.name == 'Tops') > > > all_products_that_are_tops = db(db.products.thefilter.belongs(myfilter. > _select(db.thefilter.id))) > > this basically searches all the ids of the "thefilter" table that have a > "name" == Tops. > Then passes those id to the "products" table that holds a reference to the > "thefilter" table. > > Il giorno venerdì 10 maggio 2013 17:26:30 UTC+2, brac...@gmail.com ha > scritto: >> >> How do I search for all products of a certain combination of filters in a >> many to many relationship? The products returned have to at least have the >> filters given. >> >> For example, if I had this many to many relationship: >> >> db.define_table('thefilter_products', >> Field('product', db.products), >> Field('thefilter', db.thefilter)) >> >> And say we have: >> "Product1": "Tops", "Sweatshirts" >> "Product2": "Tops", "Sweatshirts", "Hoodie" >> "Product3": "Tops", "AthleticWear" >> >> And I wanted to get all products that at least contain the filters "Tops" >> and "Sweatshirts", what would be the db().select() query to do this? The >> desired result would return only "Product1" and "Product2". >> >> I tried using belongs, but that returned all products that included any >> of the filter terms. I've also tried using the & on both the "Tops" and >> "Sweatshirts" filter id ( db.thefilter_products==thefilter.id ), but >> this returned an empty Set. >> > -- --- 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. For more options, visit https://groups.google.com/groups/opt_out.