On Fri, Nov 27, 2009 at 11:41, lea <[email protected]> wrote:
> I have the following entities:
>
> class Responder(Entity):
>    cleavage_values = OneToMany('CleavageValue')
>
> class CleavageValue(Entity):
>    cleavage = ManyToOne('Cleavage', colname='cleavage_id',
>                              onupdate='CASCADE',
>                              ondelete='CASCADE',
>                              primary_key=True)
>    responder = ManyToOne('Responder', colname='responder_id',
>                              onupdate='CASCADE',
>                              ondelete='CASCADE',
>                              primary_key=True)
>
> class Cleavage(Entity):
>    ...
>
> class SpiderCleavage(Cleavage):
>    ...
>
> class MapCleavage(Cleavage):
>    ...
>
>
> is there a simple way to filter the Responder's cleavage_values list
> to get only those cleavage_values that belong to a SpiderCleavage?
> My idea would be something like that:
>
> responder.cleavage_values.filter(CleavageValue.cleavage.cleavage_id.in
> (SpiderCleavage.query.all()))

If you want to set the filter criterion at runtime, you can use
lazy='dynamic' on the relationship, which would allow you do use
.filter on the relation directly as you did.

If you always want to filter on the same criterion, you can use the
"filter" argument. Note that the "in" approach you describe is very
inefficient. You can filter on the "row_type" column which is
automatically added when using polymorphic inheritance, and contains
the name of the class corresponding to the row. Something like this
should work (though I didn't test it):

cleavage_values = OneToMany('CleavageValue')
spider_cleavage_values = OneToMany('CleavageValue', filter=lambda c:
and_(c.cleavage_id == Cleavage.id, Cleavage.row_type ==
'spidercleavage'))

you *might* also be able to use of_type instead of checking for
row_type explicitly:

filter= lambda c: and_(c.cleavage_id == Cleavage.id,
CleavageValue.cleavage.of_type(SpiderCleavage))

-- 
Gaëtan de Menten
http://openhex.org

--

You received this message because you are subscribed to the Google Groups 
"SQLElixir" 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/sqlelixir?hl=en.


Reply via email to