Wtforms QueryselectField不传递id

时间:2015-04-28 17:45:28

标签: python-2.7 flask sqlalchemy wtforms

  • 我有两个一对多关系的表,表项目有很多报告。
  • 当用户输入报告问题时,他/她将不得不从下拉列表中选择此报告所属的项目。
  • 下拉列表显示项目名称,但add_report()在db.session.commit()处停止,当我打印_form.project_list.data_时,我得到项目名称而不是外键。
  • 我相信我的问题是在表单中,我尝试了很多代码来获取project_id但是我得到了“unprintable InterfaceError object”错误。
  • 我的问题: 如何从下拉列表中获取项目ID 编号而不是项目名称?

views.py

@app.route('/add_report/', methods=['GET', 'POST'])
def add_report():
    form = AddReportForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            new_report = Report(
                project_id=form.project_list.data,
                issue=form.issue.data)
            db.session.add(new_report)
            db.session.commit()
            flash('New report was successfully added.')
            return redirect(url_for('projects'))
        else:
            flash('All fields are required.')
            return redirect(url_for('projects'))
    return render_template('project.html', form=form)

Models.py

class Project(db.Model):
    project_id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    date = db.Column(db.Date)
    reports = db.relationship('Report', backref='project', lazy='dynamic')

    def __repr__(self):
        return self.name

class Report(db.Model):
    report_id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('project.project_id'))
    issue = db.Column(db.Text)

    def __repr__(self):
        return self.issue

forms.py

def get_projects():
    return Project.query

class AddReportForm(Form):
    project_list = QuerySelectField('Project', query_factory=get_projects)
    issue = StringField('issue')

非常感谢

2 个答案:

答案 0 :(得分:0)

报告表?你的意思是下拉列表吗?你试过吗

project_list = QuerySelectField('Project', query_factory=get_projects, get_label='project_id')

选中此question可将查询缩小为仅选择所需的列。

答案 1 :(得分:0)

最后我发现我的代码出了什么问题:)

  • 我在报表中使用project_id(外键)来获取项目ID(form.project_list.data)而不是项目(关系)。所以我在models.py和views.py上做了一些更改 forms.py ,我认为这是你的错!

<强> models.py

class Project(db.Model):
    project_id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    date = db.Column(db.Date)

    def __repr__(self):
        return self.name

class Report(db.Model):
    report_id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('project.project_id'))
    projects = db.relationship('Project', backref='report')
    issue = db.Column(db.Text)

    def __repr__(self):
        return self.issue

<强> views.py

@app.route('/add_report/', methods=['GET', 'POST'])
def add_report():
    form = AddReportForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            new_report = Report(
                projects=form.project_list.data,
                issue=form.issue.data)
            db.session.add(new_report)
            db.session.commit()
            flash('New report was successfully added.')
            return redirect(url_for('projects'))
        else:
            flash('All fields are required.')
            return redirect(url_for('projects'))
    return render_template('project.html', form=form)

forms.py 保持不变