8

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

5
  • You should re-read the part where you explain what happens when you remove the class attribute. Currently it does not make sense.
    – Selcuk
    Commented Feb 14, 2016 at 4:37
  • Yeah thanks, have changed it
    – andiwin
    Commented Feb 14, 2016 at 6:00
  • Can you specify how you're initializing the instance and also where transaction_label and self.transaction_label are defined?
    – wilfo
    Commented Feb 14, 2016 at 8:42
  • Edited the answer, it is in there, how I specify the instance constructor. About transaction_label it is similar to how transaction_items behaves, for now I just removed it to avoid duplicates
    – andiwin
    Commented Feb 14, 2016 at 9:01
  • Wilfo is absolutely right, it is because Form being a metaclass. I joined stackoverflow just now to ask andiwinta if they could post their new PurchaseFOrm(BaseForm). I have the same problem when trying to use the super().__init__(), and trying to extend the Form class. Much Appreciated. Commented Sep 26, 2016 at 7:26

1 Answer 1

5

To start with, I think your super().__init__(*args, **kwargs) is not valid and should be super(PurchaseForm, self).__init__(*args, **kwargs) - we're you able to run your code like that?

Also, how can you tell it's not working - looking into a created form from this code - it looks ok:

class ItemForm(Form):
    openid = StringField('openid', validators=[])
    remember_me = BooleanField('remember_me', default=False)

class PurchaseForm(Form):
    item_class = ItemForm
    transaction_items = None

    def __init__(self, item_class, *args, **kwargs):
        super(PurchaseForm, self).__init__(*args, **kwargs)
        self.item_class = item_class
        self.transaction_items = FieldList(FormField(self.item_class),
                                           label='items',
                                           min_entries=1)
3
  • 1
    I think super().__init__() is valid for Python 3. I followed your example, but it is still the same, it is compiled okay but jinja2 throws error (sorry just mention it now). So the error from jinja2 is TypeError: 'UnboundField' object is not iterable and it is from {% for item in form.transaction_items %}
    – andiwin
    Commented Feb 14, 2016 at 10:31
  • 1
    look at the reply to this question - it looks like what you're trying to do is not a good practice from the point of view of WTForms
    – wilfo
    Commented Feb 14, 2016 at 10:53
  • ahh, i see so that how it works. I'll just split it for each type of form then. Thanks!
    – andiwin
    Commented Feb 14, 2016 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.