WTForm:使用SelectField的FieldList,我该如何渲染?

时间:2014-06-19 00:03:34

标签: python flask wtforms flask-wtforms

我有这个订单表格,允许我的用户创建订单。订单由多个(producetype, quantity)元组组成。 Producetype应以<select>形式呈现,而数量只能是输入。应该动态添加producetype的选择,因为这可能会改变。目前,我用裸html写了这个

enter image description here

我想使用WTForm,因为WTForm确实简化了我的代码。但是,我无法这样做:

代码:

class OrderEntryForm(Form):
  quantity = IntegerField('Quantity',
                          [validators.Required(), validators.NumberRange(min=1)])
  # we will be dynamically adding choices
  producetype = SelectField('Produce',
                            [validators.Required()],
                            choices=[])

class OrderForm(Form):
  name = TextField('Crop', [validators.Length(min=3, max=60)])
  due_date = DateField('Due Date', [validators.required()])
  order_entries = FieldList(FormField(OrderEntryForm))

我有以下问题:

  1. 如何动态地将选项添加到OrderForm的order_entries字段?
  2. 如果我有订单order = { name:hello, due_date:2014-06-18, order_entries:[ {producetype_id: 1, quantity: 2}, {producetype_id: 3, quantity: 4}] },如何使用正确的OrderForm值填充OrderEntryForm
  3. 我的代码可在此处找到:https://gist.github.com/vicngtor/f8c8f0519dbd6b3b6110

3 个答案:

答案 0 :(得分:5)

  

如何动态地将选项添加到OrderForm的order_entries字段

这取决于你的意思

如果您的意思是要在渲染后向表单添加行项目。您必须使用DOM操作在客户端添加这些操作。 WTForms 具有用于处理表单字段上的索引的命名约定。它只是name="<form-field)name>-<index>"。如果使用遵循此约定的javascript添加名称​​ WTForms 将知道如何在后端处理它。

如果您的意思是希望拥有动态选择列表,那么您可以在实例化之后迭代表单中的FieldList。您可以分配choices任何可迭代的2元组。在下面的示例中,我直接分配它们,但它们可以很容易地从存储中检索。

order_form = OrderForm()
for sub_form in order_form.order_entries:
    sub_form.producetype.choices = [('2', 'apples'), ('2', 'oranges')]
  

如何使用正确的OrderEntryForm值填充我的OrderForm?

您可以使用obj关键字参数将对象直接绑定到表单。 WTForms 足够聪明,可以根据对象的属性动态构建表单。

from wtforms import Form, IntegerField, SelectField, TextField, FieldList, FormField
from wtforms import validators
from collections import namedtuple

OrderEntry = namedtuple('OrderEntry', ['quantity', 'producetype'])
Order = namedtuple('Order', ['name', 'order_entries'])

class OrderEntryForm(Form):
  quantity = IntegerField('Quantity',
                          [validators.Required(), validators.NumberRange(min=1)])
  # we will be dynamically adding choices
  producetype = SelectField('Produce',
                            [validators.Required()],
                            choices=[
                                (1, 'carrots'),
                                (2, 'turnips'),
                            ])

class OrderForm(Form):
  name = TextField('Crop', [validators.Length(min=3, max=60)])
  order_entries = FieldList(FormField(OrderEntryForm))

# Test Print of just the OrderEntryForm
o_form = OrderEntryForm()
print o_form.producetype()

# Create a test order
order_entry_1 = OrderEntry(4, 1)
order_entry_2 = OrderEntry(2, 2)

order = Order('My First Order', [order_entry_1, order_entry_2])

order_form = OrderForm(obj=order)

print order_form.name
print order_form.order_entries

上面的示例创建了一个示例Order,并将其提供给obj关键字。在渲染时,这将生成以下(无样式):

enter image description here

答案 1 :(得分:1)

对于其他被SelectFields + FieldLists困扰的人- 这就是我用FieldList实现SelectFields的方式(启发dby nsfyn55's answer)。这种方法会根据JSON日期向/home路由动态呈现任意数量的SelectField。

路线(routes.py):

@app.route('/home', methods=['POST', 'GET'])
def home():
    custom_metadata = data

    select_metadata_form_list = SelectFormList()
    select_metadata_form_list.select_entries = get_select_entries()

    context = {
        "select_metadata_form_list": select_metadata_form_list,
    }

    return render_template('home.html', **context)

表单(forms.py):

class SelectForm(FlaskForm):
    select = SelectField("Placeholder", choices=[])


class SelectFormList(FlaskForm):
    select_entries = FieldList(FormField(SelectForm))

模板home.html

{% for select_form in select_metadata_form_list.select_entries %}
    {{select_form.select.label}}: {{ select_form.select}}
{% endfor %}

辅助方法(app.py):

def get_select_entries():
    """
    Converts custom metadata to a forms.SelectForm(), which can then be
    used by SelectFormlist() to dynamically render select items.

    :return: <forms.SelectForm object>
    """
    select_data = get_select_data_from_custom_metadata()

    select_data_labeled = get_labled_select_data(select_data=select_data)

    all_select_items = []
    for select_dict in select_data_labeled:
        for k, v in select_dict.items():
            select_id = uuid.uuid1()   # allows for multiple selects
            select_entry = SelectForm()
            select_entry.select.label = k
            select_entry.id = select_id
            select_entry.select.choices = v
            all_select_items.append(select_entry)

    return all_select_items
def get_select_data_from_custom_metadata():
    """
    [
    {"Seniority": ["Intern", "Associate", "Senior"]}
    ]
    :return: List of dictionaries containing key and list of select values
    """
    type = "select"
    select_data = []
    custom_metadata = get_custom_metadata()

    for field in custom_metadata["fields"]:
        if field["type"] == type:
            select_data.append({field["key"]: field["data_list"]})

    return select_data

“数据” (custom_metadata.json):

{
  "fields": [
    {
      "type": "text",
      "key": "Position"
    },

    {
      "type": "select",
      "key": "Seniority",
      "data_list": ["Intern", "Associate", "Senior", "Executive"]
    },
    {
      "type": "select",
      "key": "Company",
      "data_list": ["Ford", "Chevy", "Toyota"]
    },
    {
      "type": "select",
      "key": "Team",
      "data_list": ["Bucks", "Raptors", "Thunder"]
    }
  ]
}

结果

enter image description here

答案 2 :(得分:0)

SubmitField添加OrderForm

submit_something = SubmitField((u'Add something'))

然后从您的视图中调用它,并使用append_entry的{​​{1}}方法:

FieldList

希望有所帮助!