Flask-sqlalchemy列别名用于浏览器输出 [英] Flask-sqlalchemy column alias for browser output
本文介绍了Flask-sqlalchemy列别名用于浏览器输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用for循环来输出单个数据库行的列和值.一切正常,但是有几个问题.列名不适合在浏览器中输出,因此我正在寻找一种关联别名的方法(不确定这是正确的术语)
I'm using a for loop to output the columns and values of a single database row. This is all working but there are a couple of issues. The column names aren't suitable to output in the browser so I'm looking for a way to associate an alias (not sure this is the correct term)
例如列名称:
cust_name
cust_area
所需的输出:
Customer name
Customer area
models.py
models.py
class Customers(db.Model):
id = db.Column(db.Integer, primary_key = True)
cust_name = db.Column(db.String(64))
cust_area = db.Column(db.String(64))
cat_id = db.Column(db.Integer(8), index = True)
views.py
customer = Customers.query.filter_by(cat_id = page).first()
test_dict = dict((col, getattr(test, col)) for col in test.__table__.columns.keys())
return render_template('test.html',
customer = test_dict
)
test.html
test.html
{% for key, value in customer.items() %}
{{ key }} : {{ value }}
{% endfor %}
谢谢!
推荐答案
Then you could iterate through columns like following:
customer = Customers.query.filter_by(cat_id=page).first()
data = dict((c.info.get('name', c.name), getattr(customer, c.name))
for c in customer.__table__.c)
# Or using dict comprehension syntax (Python 2.7+).
data = {c.info.get('name', c.name): getattr(customer, c.name)
for c in customer.__table__.c}
这篇关于Flask-sqlalchemy列别名用于浏览器输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文