简体   繁体   中英

'_BoundDeclarativeMeta' object is not iterable' error in Flask

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 models.py file I'm working from looks like this:

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

The database is populated the way I'd like it to be, so the problem isn't there. I'm getting the _BoundDeclarativeMeta error from my views.py file, which looks like this:

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)

I can gather from the error message that when I refer to Email in views.py , 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?

I'm a dope - it was an easy fix. I got it to work by changing the posts list comprehension to posts = [i.body for i in Email.query.all()] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM