Created
June 1, 2011 16:29
-
-
Save creiht/1002693 to your computer and use it in GitHub Desktop.
Cool recipe that I found online for making sql alchemy objects dictable
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
from sqlalchemy.orm import object_mapper | |
class DictableAlchemy(object): | |
def __iter__(self): | |
self._iter = iter(object_mapper(self).columns) | |
return self | |
def next(self): | |
name = self._iter.next().name | |
return name, getattr(self, name) | |
# Wrap into the declarative base as so: | |
metadata = MetaData() | |
ModelBase = declarative_base(metadata=metadata, cls=DictableAlchemy) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment