Skip to content

No way to build a dynamic form with inheritance #736

@benthorner

Description

@benthorner

Expected Behavior

We have some data like the following:

[
  {'id': 'item1', 'percentage': 20},
  {'id': 'item2', 'percentage': 50},
  {'id': 'item3', 'percentage': 30},
]

We want to make a form with an input for each item so that we can change all the percentages in one go and check they still add up to 100%. The number of items will change over time based on what's in our database, so we can't hard-code them as class-level attributes and need to specify the fields dynamically instead.

In theory we could do this with the BaseForm class, passing an unbound field for each input in the constructor. However, this isn't possible for us in practice as we're using the FlaskForm class from flask_wtf, which inherits from Form. The Form class locks the set of fields to the class-level attributes discovered by its metaclass.

Ideally we would do this:

class DynamicFieldForm(Form): 
  # item_1 = InputField(item_1_id)  # can't do this
  # code to do validation, etc.
  pass

form = DynamicFieldForm(
  extra_fields=[
    InputField(item['id']) for item in items
  ],
  data={
    item['id']: item['percentage'] for item in items
  }
)

Actual Behavior

It's possible to pass fields in the constructor with this hack:

# toy example without validation
class DynamicFieldForm(Form):
    def __init__(self, items):
        self._unbound_fields = [
          (item['id'], InputField(item['id'])
          for item in items
        ]

        super().__init__(data={
            item['id']: item['percentage'] for item in items
        })

...

# get data for an item later on
getattr(form, item['id']).data

Using the internal _unbound_fields attribute isn't great as it's not part of the public interface and might break in future versions. Would it be possible to expose it in the constructor for the Form class?

Environment

  • Python version: 3.9
  • wtforms version: 3.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature, or existing feature improvement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions