'_BoundDeclarativeMeta' object is not iterable' error in Flask

时间:2016-02-12 21:34:24

标签: python flask flask-sqlalchemy

I'm trying to create a Flask app that will display the amount and content of emails sent by Presidential campaigns. I'm using Flask-SQLAlchemy to create a database to hold the emails. The fname file I'm working from looks like this:

models.py

The database is populated the way I'd like it to be, so the problem isn't there. I'm getting the from app import db class Politician(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), index=True) emails = db.relationship('Email', backref='author', lazy='dynamic') def __repr__(self): return '<Politician %s>' % self.name class Email(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(10000)) sender_id = db.Column(db.String(10000), db.ForeignKey('politician.name')) def __repr__(self): return '<E-mail %s>' % self.body error from my _BoundDeclarativeMeta file, which looks like this:

views.py

I can gather from the error message that when I refer to from flask import render_template, flash, redirect from app import app from .models import Politician, Email @app.route('/') @app.route('/index') def index(): posts = [i.body for i in Email] return render_template('index.html', title='Home', posts=posts) in Email, I shouldn't use the model name, but I'm not quite sure how to access the emails themselves from the database. Do any of you have any suggestions?

1 个答案:

答案 0 :(得分:0)

I'm a dope - it was an easy fix. I got it to work by changing the posts list comprehension to new <classname>.