2

This code works fine separately. What I mean is when I set the default tag and call process() all the other data that should populate the form have been removed. In this case the default is ok, but the other fields are empty.

form = ReviewForm(**populate_form)
form.tags.default = '1'
form.process()

So, it seems like process cleans the **populate_form values out. I need to populate all fields and then set the default for the select.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
anvd
  • 3,997
  • 19
  • 65
  • 126
  • 2
    Please rephrase your question. Try to explain your problem keeping in mind we know nothing of your code except what you are showing us. – Alexandre Bell Feb 20 '15 at 01:33
  • 1
    I think you should be setting the default value when instantiating the form field, not after populating a form instance. See https://wtforms.readthedocs.org/en/latest/fields.html – augurar Feb 20 '15 at 01:37
  • 1
    @AlexandreBell data gets removed when process() is executed. Here is a solution if you see the code. but this is terrible if the form is big (my case). http://librelist.com/browser//flask/2013/5/3/flask-wtforms-csrf-token-missing-when-using-selectmultiplefield/ – anvd Feb 20 '15 at 01:49

1 Answers1

0

From the WTForms Documentation:

Since BaseForm does not take its data at instantiation, you must call this to provide form data to the enclosed fields. Accessing the field’s data before calling process is not recommended.

But the Form subclass allows you to pass in objects as kwargs, which you are doing here. However, looking at the source, it looks like what is happening here is that __init__ is calling process to process the fields, so I think there is some overriding going on.

In any case, I think what is missing here is that the default value should be defined upon definition, not after instantiation. Look at the construction as well as this answer, and I think that will clear things up.

Community
  • 1
  • 1
pswaminathan
  • 8,734
  • 1
  • 20
  • 27