When I examine the results of a query, it looks like a list of lists. I want to return a list of dicts mapping column names to result values. How can I convert the result rows to dicts?
results = db.session.query(
PendingPost.campaign_id.label('campaign_id'),
Campaign.title.label('title'),
sqlalchemy.func.count(PendingPost.status).label('status_count'),
).join(
Campaign, Campaign.id == PendingPost.campaign_id,
).join(
Areas, Areas.id == PendingPost.area_id
).filter(
sqlalchemy.func.month(PendingPost.creation_date) == datetime.datetime.utcnow().month
).group_by(
PendingPost.status,
PendingPost.campaign_id,
).all()
print(results)
[(3, 'campaign title', 1),
(4, 'campaign title', 1)]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…