我試圖在現有工作動態字段(客戶)中添加一個空白選項,其結果是在SOF here上找到的結果,但是出現錯誤。嘗試在動態WTForm字段中插入空白選項
的錯誤是ValueError: invalid literal for int() with base 10: ''.
如果必要的話,我可以提供完整的回溯。
這裏是形式 - 充滿活力的領域是客戶一個你可以看到:
class FilterWorkorderForm(FlaskForm):
id = IntegerField('id', validators=[Optional()])
date = DateField('Date', validators=[Optional()])
customer = SelectField('Customer', coerce=int, validators=[Optional()])
customer_po = StringField('Customer PO', validators=[Optional()])
requested_by = StringField('Requested By', validators=[Optional()])
work_description = StringField('Work Description', validators=[Optional()])
status = SelectField('Status', choices=[('Quote', 'Quote'), ('Pending', 'Pending'), ('WIP', 'WIP'), ('Complete', 'Complete'), ('TBI', 'TBI'), ('Invoiced', 'Invoiced'), ('VOID', 'VOID')])
下面是路線:
@app.route('/reports/filter_workorder', methods=['GET', 'POST'])
@login_required
def filter_workorder():
results = None
form = FilterWorkorderForm()
form.customer.choices = [(cus.id, cus.company_name) for cus in Company.query.order_by('id')]
### LINE CAUSING ERROR ### form.customer.choices.insert(0, ("", "")) ### LINE CAUSING ERROR ###
if request.method == 'POST':
if form.validate_on_submit():
try:
customer_id = form.customer.data
customer = Company.query.filter_by(id = customer_id).first_or_404()
customer_name = customer.company_name
filter_data = {'id' : form.id.data, 'date' : form.date.data, 'customer_po' : form.customer_po.data, 'customer' : customer_name,
'work_description' : form.work_description.data, 'status' : form.status.data}
filter_data = {key: value for (key, value) in filter_data.items() if value}
results = Workorder.query.filter_by(**filter_data).all()
except Exception as e:
db.session.rollback()
flash(e)
return render_template('filter_workorder.html', form = form, results = results)
return render_template('filter_workorder.html', form = form)
這是正確的!謝謝。我應該能夠自己解決這個問題。只需要調整我的路線處理來處理更改。可以了,好了。 – xGlorify