Created
March 16, 2014 18:49
-
-
Save doc-hex/9587966 to your computer and use it in GitHub Desktop.
Flask-Sqlalchemy monkey patch to simplify debug with: DBObjectClassName[pk_value]
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
# Hack to allow me to type just ClassName[34] and fetch DB object with pk=34 | |
# | |
from flask.ext.sqlalchemy import _BoundDeclarativeMeta | |
def _get_by_pk(cls, idx): | |
return cls.get(idx) | |
_BoundDeclarativeMeta.__getitem__ = _get_by_pk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, that uses a base class function ".get(pk)" that I use on all my objects.
Change line 5 to be "
return cls.query.get(idx)
" if you don't have that.