I'm trying to write a Flask application that queries my database (using peewee), and sends the results into Jinja2 for rendering. My view looks like this:
@application.route('/maindash')
def maindash():
sq = Selfreported.select().join(Demog).where(Demog.mrn == session['mrn']).order_by(Selfreported.reportingdate)
series1data = ["Series 1", ([s.reportingdate, s.series] for s in sq)]
jsonedData = json.dumps(series1data)
return render_template('maindash.html', seriesdata=jsonedData)
To pass the results from the query into Jinja2, I know that I need to serialize the data. But when I call json.dumps: TypeError: at 0x104854be0> is not JSON serializable. So I guess I am not actually getting the data, but trying to serialize the query object itself?
Every peewee example I've looked at uses the object_list helper function from peewee-flask, rather than passing query results straight into render_template. I've looked at object_list, but I'm having a hard time understanding it --
def object_list(template_name, qr, var_name='object_list', **kwargs):
kwargs.update(
page=int(request.args.get('page', 1)),
pages=qr.count() / 20 + 1
)
kwargs[var_name] = qr.paginate(kwargs['page'])
return render_template(template_name, **kwargs)
Is there a method on a QueryResultWrapper I should be using to get the actual data rather than objects? I've looked at tuples() and dict(), but I couldn't iterate over them.