Flask QuerySelectField编辑表单

时间:2018-05-28 10:30:09

标签: flask drop-down-menu flask-sqlalchemy flask-wtforms

我在表单中有以下查询选择字段,它将在插入操作过程中从数据库中选择值。 插入完成插入记录将显示在上,并带有“编辑”选项。 在单击编辑时,我希望表格预先填充所选记录。

在我的情况下,我有两个查询选择字段,其中需要预先填充。 我正在努力做到这一点,我没有太大的成功。 你能指导一下我做错了吗?

表单代码

def choice_pername():
    return Persons.query.filter_by(u_id=current_user.id)
    #return Persons.query.with_entities(Persons.per_name).filter_by(u_id=current_user.id)

def choice_eartype():
    return EarType.query.filter_by(u_id=current_user.id)

class EarningEntryForm(FlaskForm):
    Ear_per_name = QuerySelectField(query_factory=choice_pername,allow_blank=False,get_label='per_name',)
    Ear_type_name = QuerySelectField(query_factory=choice_eartype,allow_blank=False,get_label='EarType_name')
    Ear_amt = FloatField('Earning Amount:-',validators=[DataRequired()])
    Ear_date = DateField('Earning Date:-',format = '%Y-%m-%d',validators=[DataRequired()])
    Ear_FileName = StringField('Earning FileName:-')
    Ear_img = FileField('Earning Proof File:-')
    Ear_comm = TextAreaField('Earning Comment:-',validators=[DataRequired()])
    submit = SubmitField('Save Earning')
    Delete = SubmitField('Delete')

代码路由

@bp.route('/earnings/edit_earn',methods=['GET','POST'])
@login_required
def edit_earn():
    earID = request.args.get("earn_id")
    earnings = Earnings.query.filter_by(id= earID,U_id=current_user.id).all()
    form = EarningEntryForm()
    meth = request.method
    if form.validate_on_submit():
        earnings.Ear_per_name =str(form.Ear_per_name.data)
        earnings.Ear_type_name =str(form.Ear_type_name.data)
        earnings.Ear_amt = form.Ear_amt.data
        earnings.Ear_date = form.Ear_date.data
        earnings.Ear_FileName = form.Ear_img.data.filename
        earnings.Ear_img = form.Ear_img.data.read()
        earnings.Ear_comm =form.Ear_comm.data
    elif request.method == 'GET':
        Earid = EarType.query.filter_by(EarType_name =earnings[0].Ear_type_name).all()
        Perid = Persons.query.filter_by(per_name =earnings[0].Ear_per_name).all()
        #form = EarningEntryForm(Ear_per_name=Perid[0].id, Ear_type_name=Earid[0].id)
        form.Ear_type_name.data = Earid
        form.Ear_per_name.data= Perid
        form.Ear_amt.data = earnings[0].Ear_amt
        form.Ear_date.data = earnings[0].Ear_date
        form.Ear_FileName.data = earnings[0].Ear_FileName
        form.Ear_comm.data = earnings[0].Ear_comm
    return render_template('earning/earn_Edit.html', form=form, ear=earnings)

代码Jinja模板

<div class="col-md-4">
 <h1>E2ISA Earning Edit</h1>

<form class="form-inline" method="post">
{{ form.hidden_tag() }}
{{ form.csrf_token }}

    <input type="text" name="ear_id" value="{{ear.id}}" hidden>

<p>
Selected Person Name:<br>
{{ form.Ear_per_name(class='btn btn-secondary dropdown-toggle')}}
{% for error in form.Ear_type_name.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>

<p>
Selected Earning Type:<br>
{{ form.Ear_type_name(class='btn btn-secondary dropdown-toggle')}}
    {% for error in form.Ear_type_name.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>

<p>
{{ form.Ear_amt.label }}<br>
{{ form.Ear_amt(size=32,class='form-control') }}
    {% for error in form.Ear_amt.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>

<p>
{{ form.Ear_date.label }}<br>
{{ form.Ear_date(class='datepicker') }}
    {% for error in form.Ear_date.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>

<p>
{{ form.Ear_FileName.label }}<br>
{{ form.Ear_FileName (class='form-control')}}
    {% for error in form.Ear_FileName.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.Ear_comm.label }}<br>
{{ form.Ear_comm (class='form-control') }}
    {% for error in form.Ear_comm.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>

<p>{{ form.submit(class='btn btn-primary') }}</p>
</form>
<form class="form-inline" action="/earnings/DeleteEarn">
{{ form.csrf_token }}
<input type="text" name="ear_id" value="{{ear.id}}" hidden>

{{ form.Delete(class='btn btn-primary') }}
</form>

</div>
{% endblock %}

这是DB模型 enter image description here

1 个答案:

答案 0 :(得分:1)

我使用以下代码

解决了这个问题
form.Ear_type_name.data =int(Earid[0].id)
form.Ear_per_name.data = int(Perid[0].id)

上面的代码是传递各个选定字段的索引值以形成选择字段类型,在我的情况下它是1|Nirav所以我必须传递1来选择字段表单数据类型。

在表单中,我使用了选择字段而不是查询选择字段。

Ear_per_name=SelectField('PersonName', choices=[], coerce=int)
Ear_type_name=SelectField('EarningType Name', choices=[], coerce=int)

您可以使用以下方法从数据库加载此字段

person=Persons.query.filter_by(u_id=current_user.id)
eartype = EarType.query.filter_by(u_id=current_user.id)
eartype_list = [(j.id, j.EarType_name) for j in eartype]
person_list = [(i.id, i.per_name) for i in person]
form.Ear_per_name.choices = person_list
form.Ear_type_name.choices = eartype_list

以下是我更新的编辑方法代码路线

@bp.route('/earnings/edit_earn',methods=['GET','POST'])
@login_required
def edit_earn():
    earID = request.args.get("earn_id")
    earnings = Earnings.query.filter_by(id= earID,U_id=current_user.id).all()
    form = EarningEntryForm(request.form,obj=earnings)
    meth = request.method
    if form.validate_on_submit():
        earnings.Ear_per_name =str(form.Ear_per_name.data)
        earnings.Ear_type_name =str(form.Ear_type_name.data)
        earnings.Ear_amt = form.Ear_amt.data
        earnings.Ear_date = form.Ear_date.data
        earnings.Ear_FileName = form.Ear_img.data.filename
        earnings.Ear_img = form.Ear_img.data.read()
        earnings.Ear_comm =form.Ear_comm.data

    elif request.method == 'GET':
        #THis code will load the dropdown box.
        person = Persons.query.filter_by(u_id=current_user.id)
        eartype = EarType.query.filter_by(u_id=current_user.id)
        eartype_list = [(j.id, j.EarType_name) for j in eartype]
        person_list = [(i.id, i.per_name) for i in person]
        form.Ear_per_name.choices = person_list
        form.Ear_type_name.choices = eartype_list

        #This where edit form take place.
        Earid = EarType.query.filter_by(EarType_name =earnings[0].Ear_type_name).all()
        Perid = Persons.query.filter_by(per_name =earnings[0].Ear_per_name).all()
        form.Ear_type_name.data =int(Earid[0].id)
        form.Ear_per_name.data = int(Perid[0].id)
        form.Ear_amt.data = earnings[0].Ear_amt
        form.Ear_date.data = earnings[0].Ear_date
        form.Ear_FileName.data = earnings[0].Ear_FileName
        form.Ear_comm.data = earnings[0].Ear_comm
    return render_template('earning/earn_Edit.html', form=form, ear=earnings)

Happy Codding。