Skip to content

Instantly share code, notes, and snippets.

@nom3ad
Forked from shuxiang/sqla.py
Created August 16, 2020 05:48
Show Gist options
  • Save nom3ad/52551f8596e278e72a90c5ac2af3b41e to your computer and use it in GitHub Desktop.
Save nom3ad/52551f8596e278e72a90c5ac2af3b41e to your computer and use it in GitHub Desktop.
sqlalchemy get model by name and get model by tablename
from flask.ext.sqlalchemy import SQLAlchemy
def get_model(self, name):
return self.Model._decl_class_registry.get(name, None)
SQLAlchemy.get_model = get_model
def get_model_by_tablename(self, tablename):
for c in self.Model._decl_class_registry.values():
if hasattr(c, '__tablename__') and c.__tablename__ == tablename:
return c
SQLAlchemy.get_model_by_tablename = get_model_by_tablename
db = SQLAlchemy(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment