分配数据时Wtforms DecimalField上的TypeError

时间:2018-03-16 01:07:22

标签: flask wtforms

我正在尝试从数据库填充表单,除了DecimalField之外,一切正常。我试过从数据库中分配数据,尝试分配整数,字符串如'35'和u'35'并尝试int(35)和Decimal(35)。所有这些都给出了TypeError。

表格是:

class MyBaseForm(Form):
    class Meta:
        locales = ['es_ES', 'es']

class SuscriptorForm(MyBaseForm):
    idDistribuidor= SelectField('Distribuidor', validators=[validators.InputRequired()], coerce = int)
    nombreSuscriptor= StringField('Nombre', validators=[validators.InputRequired()])
    localidad = StringField('Localidad', validators=[validators.InputRequired()])
    direccionSuscriptor = StringField('Direccion')
    telefono = StringField('Telefono')
    email = StringField('Email')
    descuento = DecimalField('Descuento', validators=[validators.Optional()], places = [0,1,2])

现在我用post方法

    form.idDistribuidor.data = suscriptor['id_distribuidor']
    form.nombreSuscriptor.data = suscriptor['nombre_suscriptor']
    form.localidad.data = suscriptor['localidad']
    form.direccionSuscriptor.data = suscriptor['direccion_suscriptor']
    form.telefono.data = suscriptor['telefono']
    form.email.data = suscriptor['email']
    form.descuento.data = suscriptor['descuento']

Suscriptor拥有此特定表单的数据库数据。它有效,除了十进制。我尝试改变以前的东西,但没有运气。知道怎么解决吗?无论从哪里来的数据我似乎无法在没有错误的情况下在decimalField中放置任何内容。 这是追溯。

    Traceback (most recent call last):
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
        return self.wsgi_app(environ, start_response)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
        response = self.handle_exception(e)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception
        reraise(exc_type, exc_value, tb)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
        raise value
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
        response = self.full_dispatch_request()
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
        raise value
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
        rv = self.dispatch_request()
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask_login/utils.py", line 261, in decorated_view
        return func(*args, **kwargs)
      File "/home/german/code/dbNoticias/app/bluAdmin/__init__.py", line 181, in editSuscriptor
        return render_template('addSuscriptor.html', activeMenuItem = '', edit = 'edit', form = form)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/templating.py", line 134, in render_template
        context, ctx.app)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/flask/templating.py", line 116, in _render
        rv = template.render(context)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
        return self.environment.handle_exception(exc_info, True)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
        reraise(exc_type, exc_value, tb)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
        raise value.with_traceback(tb)
      File "/home/german/code/dbNoticias/app/bluAdmin/templates/addSuscriptor.html", line 1, in top-level template code
        {% extends "baseCP.html" %}
      File "/home/german/code/dbNoticias/app/bluAdmin/templates/baseCP.html", line 93, in top-level template code
        {% block mainContent %}{% endblock %}
      File "/home/german/code/dbNoticias/app/bluAdmin/templates/addSuscriptor.html", line 34, in block "mainContent"
        {{ form.descuento(class_="form-control", placeholder="Descuento") }}
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/wtforms/fields/core.py", line 153, in __call__
        return self.meta.render_field(self, kwargs)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/wtforms/meta.py", line 56, in render_field
        return field.widget(field, **render_kw)
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/wtforms/widgets/core.py", line 163, in __call__
        kwargs['value'] = field._value()
      File "/home/german/code/dbNoticias/env3/lib/python3.5/site-packages/wtforms/fields/core.py", line 629, in _value
        exp = decimal.Decimal('.1') ** self.places
    TypeError: unsupported operand type(s) for ** or pow(): 'decimal.Decimal' and 'list'

1 个答案:

答案 0 :(得分:1)

回溯显示涉及小数位的问题:

exp = decimal.Decimal('.1') ** self.places TypeError: unsupported operand type(s) for ** or pow(): 'decimal.Decimal' and 'list'

您为list

提供了DecimalField个地点

descuento = DecimalField('Descuento', validators=[validators.Optional()], places = [0,1,2])

但它需要int,例如

descuento = DecimalField('Descuento', validators=[validators.Optional()], places=2)