you probably want to *return* the resuls of Base.__getattr__(self, attr), the reason it works at the moment is because __getattr__() is raising AttributeError, which is the correct answer for hasattr(yourclass, '_sa_instance_state'). As long as you handle that one attribute access transparently, using __getattr__() should be fine.
On Jan 14, 2009, at 11:09 PM, rich wrote: > > Hello, > > I discovered SQLAlchemy yesterday, and have been trying to figure out > if its worth using as I rewrite my clunky code to maintain sql records > of parameter values of expensive data structures (large numpy arrays) > stored in Python shelves. > > It looks fantastic, but not sure yet that it is the right tool. > > Because I use extensive use of __getattr__ in my classes as currently > written, I added the __getattr__ method in this sample code from this > tutorial (http://www.sqlalchemy.org/docs/05/ormtutorial.html). > > ################################################ > from sqlalchemy.ext.declarative import declarative_base > > Base = declarative_base() > class User(Base): > __tablename__ = 'users' > id = Column(Integer, primary_key=True) > name = Column(String) > fullname = Column(String) > password = Column(String) > def __init__(self, name, fullname, password): > self.name = name > self.fullname = fullname > self.password = password > > def __getattr__(self,attr): > print "called __getattr__ %s" % attr > > def __repr__(self): > return "<User('%s','%s', '%s')>" % (self.name, self.fullname, > self.password) > ##################################### > > I got this error: > >>>> u = User('jon','jon jon','jj') > called __getattr__ _sa_instance_state >>>> session.add(u) > called __getattr__ _sa_instance_state > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File ".../SQLAlchemy-0.5.0-py2.5.egg/sqlalchemy/orm/session.py", > line 1096, in add > self._save_or_update_state(state) > File ".../SQLAlchemy-0.5.0-py2.5.egg/sqlalchemy/orm/session.py", > line 1105, in _save_or_update_state > self._save_or_update_impl(state) > File ".../SQLAlchemy-0.5.0-py2.5.egg/sqlalchemy/orm/session.py", > line 1264, in _save_or_update_impl > if state.key is None: > AttributeError: 'NoneType' object has no attribute 'key' >>>> > > then I changed to: > > def __getattr__(self,attr): > print "called __getattr__ %s" % attr > Base.__getattr__(attr) > > and > >>>> session.add(u) > > runs without error. > > I'm definitely in over my head at this point, but I'd like to know if > this is a reasonable solution or if I'm going to run problems with > SQLAlchemy regarding my reliance on __getattr__ down the road. Any > pointers would be much appreciated. > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---