title: Flask prepopulate form
body: <p>I have a weird problem with flask, flask-wtforms and flask-sqlalchemy. </p>
  
  <p>I have the following form:</p>
  
  <pre><code>class CompEditForm(Form):
  
      name = StringField('Company name', validators = [Required()])
      desc = TextAreaField('Description')
      address = StringField('Adress', validators = [Required()])
      ...
  
      sectors = SelectMultipleField(u'Sectors', coerce=int)
      submit = SubmitField(u'Save changes')
  </code></pre>
  
  <p>when I write a flask view for editing the company:</p>
  
  <pre><code>def edit_company(id):
  
      comp = Company.query.get_or_404(id)
      form = CompEditForm(request.form,comp)
  </code></pre>
  
  <p>the above code prepopulates the form with all the fields like name, description etc. Since a company can operate in different sectors, I modeled that with a many-to-many relationship. I try to populate the form's SelectMultipleFiled with the following:</p>
  
  <pre><code>form.sectors.choices = [(g.id, g.name_srb) for g in Sector.query.order_by('name')]    
  </code></pre>
  
  <p>and so far so good, the form is populated with the sectors and it works nice. This is the same as the form for creating <em>new companies</em>. The problem arises when I wish to prepopulate the editing form with the existing sectors:</p>
  
  <pre><code>form.sectors.data = [c.id for c in comp.sectors.all()]
  </code></pre>
  
  <p>It populates the form with the right sectors, i.e. they are selected upon rendering, but when I submit the form, they just don't change and match the user's input. They remain the same:</p>
  
  <pre><code>if form.validate_on_submit():
  
      print form.sectors.data  # stays the same, not taking account of the user's new choice
  </code></pre>
  
  <p>Any idea? I tried with the default argument instead of data, but it does not prepopulate with any sectors.</p>