for some reason I need to declare a field inside the __init__()
so I can make arbitrary type of FormField
.
Take for example in form.py
:
class PurchaseForm(Form):
item_class = ItemForm
transaction_items = FieldList(FormField(item_class),
label='items',
min_entries=1)
def __init__(self, item_class, *args, **kwargs):
super().__init__(*args, **kwargs)
self.item_class = item_class
self.transaction_items = FieldList(FormField(self.item_class),
label='items',
min_entries=1)
If I do it like that, the transaction_items
field is not replaced by the __init__()
, can I do something to override it? or do something like setattr
for this specific instance?
Edit: Here is how I specify the constructor
import form
@app.route('/add/purchase-transaction', methods=['GET', 'POST'])
def add_purchase_transaction():
form = forms.PurchaseForm(form.ItemForm)
if form.validate_on_submit():
# do something
return render_template('add-purchase-transaction.html', form=form)
So my goal is to make a PurchaseForm
which has a FieldList
containing ItemForm
form class, and in the future I can swap ItemForm
to different class, for example to PurchaseItemForm
transaction_label
andself.transaction_label
are defined?transaction_label
it is similar to howtransaction_items
behaves, for now I just removed it to avoid duplicates