Flask-sqlalchemy Column Alias For Browser Output
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
Solution 1:
Use info
dictionary:
classCustomers(db.Model):
id = db.Column(db.Integer, primary_key=True)
cust_name = db.Column(db.String(64), info={'name': 'Customer name'})
cust_area = db.Column(db.String(64), info={'name': 'Customer area'})
cat_id = db.Column(db.Integer(8), index=True)
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}
Post a Comment for "Flask-sqlalchemy Column Alias For Browser Output"