Skip to content

Instantly share code, notes, and snippets.

@creiht
Created June 1, 2011 16:29
Show Gist options
  • Save creiht/1002693 to your computer and use it in GitHub Desktop.
Save creiht/1002693 to your computer and use it in GitHub Desktop.
Cool recipe that I found online for making sql alchemy objects dictable
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