I tested on class level and the inspect function, sth like this: inspect(FilmNode).add_property("children_lazy", relation( Film, remote_side=[FilmNode.id], lazy='dynamic', uselist=True ) )
and class FilmNode(Node): children_lazy = relation( 'Film', remote_side=[id], lazy='dynamic', uselist=True ) It worked both way, I prefer the class level definition :-). By the way, the *Film* and *FilmNode* are both sub class of Node, and FilmNode has one --> many relationship to Film. Thanks Michael!! On Monday, May 11, 2015 at 11:23:17 PM UTC+8, Michael Bayer wrote: > > > > On 5/11/15 11:16 AM, Geo wrote: > > Ok, I got this working by adding the following code into the base class: > > parent_lazy = relation( > 'Node', > remote_side=[id], > backref=backref( > 'children_lazy', > collection_class=ordering_list('position'), > order_by=[position], > cascade='all', > lazy='dynamic' ) > ) > > > The I can use node.children_lazy to get a dynamic loader. I still have > one question, am I able to add this dynamic loader by sub classing the > Node? As the base class code is from the upstream, I'm wondering if I can > have this feature without touching the base class. > > If the Node is mapped using the declarative system, then subclassing it is > going to create a new mapper which links to the original using single-table > mapper inheritance. You might be able to do that, though this would have > the wrinkle that the existing self-referential relationships on Node would > still return the superclass that does not have your attribute. > > More directly would be to add the new property to the original mapper, but > that adds it to that mapping globally, the way to do that which is agnostic > of whether or not declarative is used is > "inspect(Node).add_property("children_lazy", relationship(Node, ...))". > > > > > > > On Monday, May 11, 2015 at 10:54:24 PM UTC+8, Michael Bayer wrote: >> >> >> >> On 5/11/15 5:50 AM, Geo wrote: >> >> I have a base class node: >> >> class Node: >> >> parent = relation( >> >> 'Node', >> >> remote_side=[id], >> >> backref=backref( >> >> 'children', >> >> collection_class=ordering_list('position'), >> >> order_by=[position], >> >> cascade='all' >> >> ) >> >> ) >> >> by default the node.children give me a eager loading collection, and >> what I want is to have a subclass of the Node but only change the loading >> to lazy='dynamic' so on the sqlalchemy.orm.dynamic.AppenderQuery I can >> control the loading on the runtime. Or If I can work on the instance level? >> I read the documentation but I can't figure out setting the behaviour for >> my case. >> >> eager or lazy loading can be controlled using query loader options, >> however the "dynamic" loader specifically isn't compatible with loader >> options because it changes the behavior of the attribute at the instance >> level using an alternate descriptor. Just build a separate relationship >> that has "dynamic" as the loader strategy and use that one when you want >> that behavior. >> >> -- You received this message because you are subscribed to the Google >> Groups "sqlalchemy" group. To unsubscribe from this group and stop >> receiving emails from it, send an email to >> sqlalchemy+...@googlegroups.com. To post to this group, send email to >> sqlal...@googlegroups.com. Visit this group at >> http://groups.google.com/group/sqlalchemy. For more options, visit >> https://groups.google.com/d/optout. >> >> -- You received this message because you are subscribed to the Google > Groups "sqlalchemy" group. To unsubscribe from this group and stop > receiving emails from it, send an email to sqlalchemy+...@googlegroups.com > <javascript:>. To post to this group, send email to > sqlal...@googlegroups.com <javascript:>. Visit this group at > http://groups.google.com/group/sqlalchemy. For more options, visit > https://groups.google.com/d/optout. > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.