Created
March 12, 2013 09:15
-
-
Save wosc/5141422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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