Monday, 15 July 2013

python - How to access SQLalchemy object in jinja template using column key -



python - How to access SQLalchemy object in jinja template using column key -

i have table reference columns id, parent, number, href, when want access particular row of table, pass query object render_template method so:

render_template('home.html',\ ref = db.session.query(reference).filter(reference.parent == 1).all())

now whenever want access first returned row, use

{{ ref[0].href }}

what should alter key 0 value of number column. need access rows in template not order, column values, i.e.

{{ ref['column_value'].href }}

you load collection dictionary:

ref_by_number = dict( (row.number, row) row in db.session.query(reference).filter(reference.parent == 1) )

then can access want using ref_by_number[number] (this assumes number attributes unique).

python sqlalchemy flask jinja2

No comments:

Post a Comment