class Answer(db.Model): tablename = 'answer' id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.Text, nullable=False) question_id = db.Column(db.Integer,db.ForeignKey('question.id')) author_id = db.Column(db.Integer,db.ForeignKey('user.id')) create_time = db.Column(db.DateTime,default=datetime.now) question = db.relationship('Question',backref=db.backref('answers',order_by=id.desc())) author = db.relationship('User',backref=db.backref('answers'))
@app.route('/add_awswer/',methods=['POST']) def and_awser(): content = request.form.get('answer_content') question_id = request.form.get('question_id')
answer = Answer(content=content)
userId = session['user_id']
user = User.query.filter(User.id == userId).first()
answer.author = user
question = Question.query.filter(Question.id == question_id).first()
answer.question = question
db.session.add(answer)
db.session.commit()
return redirect(url_for('detail',question_id = question_id))
本人菜鸟,我想在模板中获取 Answer 模型中总共有多少条数据
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.