Just Launched: You can now import projects and releases from Google Code onto SourceForge
We are excited to release new functionality to enable a 1-click import from Google Code onto the Allura platform on SourceForge. You can import tickets, wikis, source, releases, and more with a few simple steps. Read More
From: Steve Zatz <slzatz@gm...> - 2005-11-18 16:31:22
My question is about filtering select objects (is that the right
term?) where there is a Many-to-Many relationship between tables.
A simple example:
categories =3D SQLRelatedJoin('Category')
name =3D StrCol(length=3D20)
items =3D SQLRelatedJoin('Item')
An Item can be associated with more than one Category and vice a versa.
While it seems like it should be very straightforward, I am not sure
what the standard way to do the following is:
1) Assume some arbitrary select object that is a collection of Item
instances. Call it SelectedItems.
2) Operate on SelectedItems to create a new select object that only
includes items in certain categories (for example, with Category names
You can, of course, do something like the following:
filtered_list_of_items =3D 
for item in SelectedItems:
item_cat_list =3D [i.name for i in item.categories]
if 'cat1' in item_cat_list or 'cat2' in item_cat_list:
This seems inefficient and doesn't produce a select object but a list
of Item instances. Is there a better way to do this with sqlobject?=20
To be more specific, is there a straightforward way to filter
SelectedItems to produce a select object that only contains items in
certain categories? Thanks for any help.
On Fri, Nov 18, 2005 at 11:30:32AM -0500, Steve Zatz wrote:
> Class Item(SQLObject)
> title= StrCol(length=75)
> categories = SQLRelatedJoin('Category')
> class Category(SQLObject)
> name = StrCol(length=20)
> items = SQLRelatedJoin('Item')
> filtered_list_of_items = 
> for item in SelectedItems:
> item_cat_list = [i.name for i in item.categories]
> if 'cat1' in item_cat_list or 'cat2' in item_cat_list:
SQLRelatedJoin gives you a SelectResult object; you can filter it
further by applying .fiilter() method to it:
item.categories.filter(Category.q.name == "cat1" || Category.q.name == "cat2" )
See sresults.py for the full list of methods...
Oleg Broytmann http://phd.pp.ru/ phd@...
Programmers don't die, they just GOSUB without RETURN.
From: Steve Zatz <slzatz@gm...> - 2005-11-19 01:49:46
> item.categories.filter(Category.q.name =3D=3D "cat1" || Category.q.nam=
e =3D=3D "cat2" )
I don't think that works but I may be misunderstanding what you are
The select object in my example consists of some collection of Item
objects so that filtering on Category.q.name doesn't appear to do the
trick. I am still not sure whether it is possible to filter on a
many-to-many attribute like the categories attribute of Item.