Skip to content

Instantly share code, notes, and snippets.

@doc-hex
Created March 16, 2014 18:49
Show Gist options
  • Save doc-hex/9587966 to your computer and use it in GitHub Desktop.
Save doc-hex/9587966 to your computer and use it in GitHub Desktop.
Flask-Sqlalchemy monkey patch to simplify debug with: DBObjectClassName[pk_value]
# 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
@doc-hex
Copy link
Author

doc-hex commented Mar 16, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment