我正在使用flask-admin和wtforms。我有一个db.Model和一个包含formField和db.Model额外字段的表单:
db.Model:
class Usuarios(db.Model):
__tablename__ = 'clienteacceso'
id = db.Column(db.INTEGER, primary_key=True)
id_cliente = db.Column(db.INTEGER, nullable=False)
usuario_acceso = db.Column(db.String, nullable=False)
activo = db.Column(db.INTEGER, nullable=False)
这是一个带有按钮的简单表单,单击该按钮可以根据usuario_acceso更新表单字段:
class AccesosForm(form.Form):
boton = SubmitField(label='editar datos de acceso'
表单(db.model带有一些额外字段)
class UsuariosForm(form.Form):
id_cliente = fields.SelectField('CUIT cliente', coerce=int,
choices=query_cliente_id_cuit()
)
usuario_acceso = fields.StringField('Usuario')
activo = fields.SelectField('Estado',
coerce=int,
choices=[
(2, 'Activo'),
(1, 'Suspendido temporalmente'),
(0, 'Baja del servicio')
]
)
acceso = fields.FormField(AccesosForm) #this is a button to update fields according to "usuario_acceso" data
# extra fields
passwd = fields.StringField('Contraseña')
plan = fields.SelectField(
'Plan',
choices=query_radgroupreply_groupname()
)
ipv4 = fields.StringField('IPv4 fija (opc)')
ipv6_link = fields.StringField('IPv6 link (recomendada)')
ipv6_delegado = fields.StringField('IPv6 delegada (opc)')
def validate(form):
if request.method == 'POST':
usuario = form.usuario_acceso.data
if form.acceso.boton.data: #this is True if "acceso" button is clicked
"""
update form fields data according to usuario_acceso
"""
else:
"""
commit form data
"""
class UsuariosAdmin(sqla.ModelView):
column_labels = dict(id_cliente = 'Cliente',
apellido = 'Apellido',
usuario_acceso = 'Usuario',
activo = 'Estado'
)
column_formatters = dict(
id_cliente = lambda v, c, m, p: id_cliente_to_nombre(id = m.id_cliente),
activo = lambda v, c, m, p: estado_to_str(int( str(m.activo).split("'")[1]))
)
column_list = ['id_cliente', 'usuario_acceso', 'activo']
column_sortable_list = column_list
column_searchable_list = column_list
column_filters = column_list
form = UsuariosForm
“验证”功能下的所有任务都可以正常工作,但是单击“保存”按钮时,由于该错误而不会显示视图:
builtins.TypeError
TypeError: populate_obj: cannot find a value to populate from the provided obj or input data/defaults
跟踪:
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask_admin/base.py", line 69, in inner
return self._run_view(f, *args, **kwargs)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask_admin/base.py", line 368, in _run_view
return fn(self, *args, **kwargs)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask_admin/model/base.py", line 2130, in edit_view
if self.update_model(form, model):
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask_admin/contrib/sqla/view.py", line 1146, in update_model
if not self.handle_view_exception(ex):
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask_admin/contrib/sqla/view.py", line 1103, in handle_view_exception
return super(ModelView, self).handle_view_exception(exc)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/flask_admin/contrib/sqla/view.py", line 1142, in update_model
form.populate_obj(model)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/wtforms/form.py", line 96, in populate_obj
field.populate_obj(obj, name)
File "/root/miniconda3/envs/flask2/lib/python3.6/site-packages/wtforms/fields/core.py", line 829, in populate_obj
raise TypeError('populate_obj: cannot find a value to populate from the provided obj or input data/defaults')