我试图在SOF here上找到一个结果后,在现有的工作动态字段(customer)中添加一个空白选项,但是得到了一个错误。在
错误是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')])
路线如下:
^{pr2}$
问题是用整数强制呈现表单,特别是customer字段。在
根据WTForms's documentation on the ^{} widget :
如果你看the source code for this method:
此强制的异常处理失败。在您的例子中,
self.coerce(value)
被执行为int('')
,这将导致您遇到的ValueError
异常。在至少有两种解决方案:
coerce
。在使用sentinel value例如}表示没有选择客户:
^{pr2}$0
或{此值将传递强制,但您将需要处理此值(以取消设置“customer”字段)的后期处理。
相关问题 更多 >
编程相关推荐