如何使用Form子类作为另一个Form子类的数据属性?

时间:2013-04-08 18:27:58

标签: python python-2.7 flask wtforms flask-wtforms

我想做这样的事情,但不知道是否有可能以任何方式:

    from flask.ext.wtf import Form, TextField
    class Foo(Form):
        a = TextField('name')
    class Bar(Form):
        b = Foo()

我得到的错误是

   Traceback (most recent call last):
   File "data_attr.py", line 4, in <module>
     class Bar(Form):
   File "data_attr.py", line 5, in Bar
     b = Foo()
   File "/usr/local/lib/python2.7/dist-packages/wtforms/form.py", line 178, in __call__
     return type.__call__(cls, *args, **kwargs)
   File "/usr/local/lib/python2.7/dist-packages/flask_wtf/form.py", line 44, in __init__
     csrf_enabled = current_app.config.get('CSRF_ENABLED', True)
   File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 336, in __getattr__
     return getattr(self._get_current_object(), name)
   File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 295, in _get_current_object
     return self.__local()
   File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line 26, in _find_app
     raise RuntimeError('working outside of application context')
   RuntimeError: working outside of application context

还有其他办法吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

您可以通过FormField

使用字段附件
class Foo(Form):
    a = TextField('name')

class Bar(Form):
    b = FormField(Foo)

http://wtforms.simplecodes.com/docs/1.0.3/fields.html#field-enclosures