Skip to content

Instantly share code, notes, and snippets.

@wosc
Created March 12, 2013 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wosc/5141422 to your computer and use it in GitHub Desktop.
Save wosc/5141422 to your computer and use it in GitHub Desktop.
def alias(column_name):
"""returns a descriptor that is usable everywhere the original column name
was: (not really a doctest, sorry)
>>> class Foo(SQLBase):
... original = Column(Integer)
... new = alias('original')
>>> foo = Foo()
>>> foo.new = 5
>>> foo.original
5
>>> session.add(foo)
>>> session.query(Foo).filter_by(new=5).count()
1
"""
# The declarative extension and sqlalchemy.orm.synonym() don't play
# together without friction by themselves, so we give them a little nudge.
def _get(self):
return getattr(self, column_name)
def _set(self, value):
setattr(self, column_name, value)
prop = property(_get, _set)
return sqlalchemy.orm.synonym(column_name, descriptor=prop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment