[英]Accessing all columns from MySQL table using flask sqlalchemy
I'm trying to get the columns for a MySQL table whether in a string or a list format. 我正在尝试以字符串或列表格式获取MySQL表的列。
Since I defined the table through my main app, I could use dir(table_name)
to get a list of attributes but those contain private attributes and other built-in attributes like "query" or "query_class". 由于我是通过主应用程序定义表的,因此可以使用
dir(table_name)
获取属性列表,但这些属性包含私有属性和其他内置属性,例如“ query”或“ query_class”。 Filtering these would be possible but I'm trying to find an easier way to get the columns without going through the attribute route. 可以对它们进行过滤,但是我试图找到一种更简单的方法来获取列而不通过属性路由。
Between reading the flask-sqlalchemy documentation and sqlalchemy documentation, I noticed that using table_name.c
will work for sqlalchelmy. 在阅读了flask-sqlalchemy文档和sqlalchemy文档之间,我注意到使用
table_name.c
可以用于sqlalchelmy。 Is there an equivalent for flask-sqlalchemy? flask-sqlalchemy是否有等效项?
Flask-SQLAlchemy does nothing to the SQLAlchemy side of things, it is just a wrapper to make it easier to use with Flask along with some convenience features. Flask-SQLAlchemy对SQLAlchemy方面没有任何作用,它只是一个包装,使它更易于与Flask一起使用以及一些便利功能。 You can inspect orm objects the same way you normally would.
您可以像平常一样检查orm对象 。 For example, inspecting a model returns its mapper .
例如, 检查模型将返回其mapper 。
m = db.inspect(MyModel)
# all orm attributes
print(list(m.all_orm_descriptors.keys()))
# just the columns
print(list(m.columns.keys()))
# etc.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.