dimanche 26 avril 2020

'list' object has no attribute 'id'

i get 'list' object has no attribute 'id' error. I dont know why.

@posts.route("/post/<int:post_id>", methods=['GET','POST'])
@login_required 
def course_post(post_id):
    post=Post.query.get_or_404(post_id)
    chapters=Chapter.query.filter_by(post_id=post_id).all()
    chapter_id = chapters.id
    videos=Videos.query.filter_by(chapter_id=chapter_id).all()
    return render_template('course.html', title=post.course_name, post=post, chapters = chapters)
class Chapter(db.Model):
    id = db.Column(db.Integer, primary_key= True)
    chapter_no = db.Column(db.Integer)
    chapter_name = db.Column(db.String(50))
    chapter_desc = db.Column(db.String(50))
    chapter_date = db.Column(db.DateTime, default=datetime.utcnow)
    vi = db.relationship('Videos', backref='topic', lazy=True)
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)

    def __repr__(self):
        return f"Chapter('{self.chapter_no}', '{self.chapter_name}', '{self.chapter_desc}', '{self.chapter_date}', '{self.post_id}'"





class Videos(db.Model):
    id = db.Column(db.Integer, primary_key= True)
    video_no = db.Column(db.Integer)
    video_name = db.Column(db.String(50), nullable=False, index=True)
    video_file = db.Column(db.String(200))
    yout_code = db.Column(db.String(200))
    video_description = db.Column(db.Text(100))
    uploaded_date = db.Column(db.DateTime, default=datetime.utcnow)
    rating = db.Column(db.Integer())
    chapter_id = db.Column(db.Integer, db.ForeignKey('chapter.id'), nullable=False)
    c_v = db.relationship("Chapter", foreign_keys=chapter_id)  #


    def __repr__(self):
        return f"Videos('{self.video_no}','{self.video_name}', '{self.video_file}', '{self.yout_code}', '{self.video_description}', '{self.uploaded_date}', '{self.rating}', '{self.chapter_id}'"

i am trying to get all the videos for each chapter in the Videos table using chapters.id as foreign key Please what am i doing wrong ?




Aucun commentaire:

Enregistrer un commentaire