I want to modify the results from a flask-sqlalchemy query by before sending them to render_template().
The view queries and modifies the items:
items = Items.query
for i in items:
date = i.__dict__['date']
i.__dict__['date_modified'] = date.replace('/', '-')
return render_template('index.html', items=items)
When the template outputs the items:
{% for i in items %}{{i.date_modified}}{% endfor %}
This error is shown in the debugger:
KeyError: 'date_modified'
However, if I modify the initial query to get a pagination object, the error is not raised and the modified date appears.
items = Items.query
items = items.paginate(page_num, items_per_page, True).items
for i in items:
date = i.__dict__['date']
i.__dict__['date_modified'] = date.replace('/', '-')
return render_template('index.html', items=items)
What's the difference between the Query result and the Pagination result that allows the latter to be manipulated in place but not the former? How can I modify the results without modifying the query with paginate?