例外是AttributeError: 'StringField' object has no attribute 'wrap_formdata'
,似乎只有在我从Flask-Admin仪表板创建或编辑以下两个模型中的任何一个时才会发生:
class Experiment(db.Model):
id = db.Column(db.Integer, primary_key=True)
property_id = db.Column(db.Integer,
db.ForeignKey('property.id'),
index=True,
nullable=False)
name = db.Column(db.String, nullable=False)
start_date = db.Column(db.DateTime, nullable=True)
end_date = db.Column(db.DateTime, nullable=True)
status = db.Column(db.String, nullable=False, default='Draft')
title = db.Column(db.String)
meta = db.Column(db.String)
seed_uri = db.Column(db.String)
targets = db.Column(postgresql.JSON)
variations_json = db.Column(postgresql.JSON)
variations = db.relationship('ExperimentVariation',
backref='experiment',
cascade='save-update, merge, delete')
def __repr__(self):
repr_fmt = '<Experiment {id}, {property_id} {name}>'
return repr_fmt.format(id=self.id,
property_id=self.property_id,
name=self.name)
class ExperimentVariation(db.Model):
id = db.Column(db.Integer, primary_key=True)
experiment_id = db.Column(db.Integer,
db.ForeignKey('experiment.id'),
index=True,
nullable=False)
name = db.Column(db.String, nullable=False)
title = db.Column(db.String)
meta = db.Column(db.String)
def __repr__(self):
repr_fmt = '<ExperimentVariation {id}, {experiment_id} {name}>'
return repr_fmt.format(id=self.id,
experiment_id=self.experiment_id,
name=self.name)
我想知道这些关系是否有些复杂化。如果有帮助,我可以提供Property
模型,但简而言之,该模型的大多数只是提供其他模型之间的关系,包括实验模型。
快速的谷歌似乎没有出现类似的问题。我很可能忽略了一些显而易见的事情,或者可能没有完全理解关系API。
此外,为了清晰起见,我还包括了完整的回溯:
ERROR [ranksci] Exception on /admin/experiment/edit/ [GET]
Traceback (most recent call last):
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask_admin/base.py", line 69, in inner
return self._run_view(f, *args, **kwargs)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask_admin/base.py", line 368, in _run_view
return fn(self, *args, **kwargs)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask_admin/model/base.py", line 1969, in edit_view
form = self.edit_form(obj=model)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask_admin/model/base.py", line 1256, in edit_form
return self._edit_form_class(get_form_data(), obj=obj)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/wtforms/form.py", line 212, in __call__
return type.__call__(cls, *args, **kwargs)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/flask_admin/form/__init__.py", line 16, in __init__
super(BaseForm, self).__init__(formdata=formdata, obj=obj, prefix=prefix, **kwargs)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/wtforms/form.py", line 278, in __init__
self.process(formdata, obj, data=data, **kwargs)
File "/Users/max/Documents/projects/ranksci-app/virtualenv/lib/python2.7/site-packages/wtforms/form.py", line 119, in process
formdata = self.meta.wrap_formdata(self, formdata)
AttributeError: 'StringField' object has no attribute 'wrap_formdata'
答案 0 :(得分:4)
我已经找到了这个问题的根本原因:Flask-Admin构造了一个表单类,默认情况下,它是每个SQLA模型的WTForm的Form类的子类。除非你的一个列匹配WTForm的构造函数所使用的参数之一,例如meta
,否则这一切都很好。
构建所述类的逻辑是here。我不完全确定对此有什么正确的解决办法,但看起来好像Flask-Admin需要在技术上或社交上处理。例如,提前知道meta
不应该用作列名是很方便的,因为会出现这个问题。
值得指出的是,WTForm的Form构造函数也接受以下变量,因此也应该避免使用:formdata
,obj
,{{ 1}}和prefix
。
此问题的技术解决方案可能是构造一组这些变量名称,然后在上面的Flask-Admin代码中创建模型表单时显式检查它们。然后可以生成某种类型的警告,或者可以更改data
变量,使其以name
或其他类型为前缀。这不是一个完美的解决方案,因为WTForms当然可以在未来改变它的API。