Dnia 2009-09-25, Pt o godzinie 21:24 -0400, thatsanicehatyouh...@mac.com pisze: > Hello, > > I am wondering if it's possible to simplify a method I have defined. I > have a few classes that are autoloaded: > > PlatePointing, Plate, Plugging, PlPlugMapM > > All are related, and all defined in this manner: > > class Plate(Base): > __tablename__ = 'plate' > __table_args__ = {'autoload' : True} > > Later, I define my relationships. I want to define an attribute that > performs a more complicated query, and this works: > > def platePointing(self,session): > try: > pp = session.query(PlatePointing)\ > .join(Plate, Plugging)\ > .filter(Plate.pk==self.plugging.plate.pk)\ > > .filter(PlatePointing.pointing_name==self.pointing_name).one() > except sqlalchemy.orm.exc.NoResultFound: > pp = None > return pp > > PlPlugMapM.platePointing = platePointing > > > My question is, can this be written so that I don't need to pass in > the session? I don't know how else to perform the query (or return the > object) without that. > First... why don't you define this method with class definition? I don't use "reflection" but I think that it is possible...
Second... you could probably convert it to column_property And back to your question.. 1. you can have globally accessed session (like it is done in Pylons via meta.Session) http://wiki.pylonshq.com/display/pylonsdocs/Using+SQLAlchemy+with+Pylons 2. you can get session for instance using sqlalchemy.orm.session.Session.object_session http://www.sqlalchemy.org/docs/05/reference/orm/sessions.html?highlight=session#sqlalchemy.orm.session.Session.object_session 3. You can have "session-aware class" http://www.sqlalchemy.org/trac/wiki/UsageRecipes/SessionAwareMapper --- Tomasz Jezierski Tefnet --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---