Thanks, it is easy and works just as expected.
Using declarative it looks like this:
Parent.boys = relation(Child, viewonly=True,
primaryjoin=(and_(Child.pid==Parent.id, Child.bg=='b')))
Parent.girls = relation(Child, viewonly=True,
primaryjoin=(and_(Child.pid==Parent.id, Child.bg=='g')))
Note that you don't specify backref='parent' because that is already defined
on the children relation. Also added viewonly=True just to make sure SA
won't be tempted to use this relation for persisting anything; not needed
here, but could be important using the same idea in more complex
associations.
--
Mike Conley
On Fri, Mar 27, 2009 at 11:51 AM, David Gardner
<[email protected]>wrote:
> Try this:
>
> mapper(Parent, parent_table, properties = {
> 'boys':relation(Child, backref='parent',
> primaryjoin=(and_(child_table.c.pid=parent_table.c.id, child_table.c.gb
> =='b'),
> 'girls':relation(Child, backref='parent',
> primaryjoin=(and_(child_table.c.pid=parent_table.c.id, child_table.c.gb
> =='g')
> })
>
>
> Mike Conley wrote:
>
> How do I create a relation that filters the list property when accessing
> the data?
>
> class Parent(Base):
> __tablename__ = 'parent'
> id = Column(Integer, primary_key=True)
> name = Column(String)
>
> class Child(Base):
> __tablename__ = 'child'
> id = Column(Integer, primary_key=True)
> pid = Column(Integer, ForeignKey('parent.id'))
> name = Column(String)
> bg = Column(String) # b=boy, g=girl
>
> Parent.children = relation(Child, backref='parent')
>
> Gives me a relation that can be used to get a list of all children
>
>
>
>
> Question is how to specify a relation that gives a list of the boys
>
> Parent.boys = relation(Child, ??????????????????)
>
> I suspect it is something pretty straight forward, but can't figure it out
>
> --
> Mike Conley
>
>
>
>
>
> --
> David Gardner
> Pipeline Tools Programmer, "Sid the Science Kid"
> Jim Henson Creature [email protected]
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---