title: Flask / Python / WTForms validation and dynamically set SelectField choices
body: <p>I'm trying to create a simple <code>Flask</code> / <code>Python</code> one page web app that uses dynamically created choices for a SelectField.</p>
  
  <p>However, I can't get it to POST using dynamically created choices, and there's also some funny validation behaviour going on (will explain after the code)</p>
  
  <p>I created a minimum failing example here:</p>
  
  <pre><code>from flask import Flask, render_template, flash, redirect
  from flask_wtf import Form
  from wtforms import IntegerField, SubmitField, SelectField
  from wtforms.validators import DataRequired, NumberRange, Optional
  
  # Set up app and config    
  DEBUG = True
  SECRET_KEY = '42721564'
  app = Flask(__name__)
  app.config.from_object(__name__)
  
  # Main stuff starts here
  
  class SelectFieldForm(Form):
      default_field = SelectField('Default Set SelectField', choices=[(i, i) for i in range(0,60,5)], coerce=int)
      default_field_2 = SelectField('Default Set SelectField', choices=[(i, i) for i in range(0,60,5)], coerce=int)
      dynamic_field = SelectField('Dynamically Set Selectfield', choices=[], validators=[Optional()], coerce=int)
  
      get_default_field_value_difference = SubmitField(label='Get Default Field Difference')
      deduct_dynamic_field_value = SubmitField(label='Deduct Dynamic Field Value')
  
  
  @app.route('/mfe-dynamic-selectfield', methods=['GET', 'POST'])
  def failingSelectField():
      form = SelectFieldForm()
  
      if form.validate_on_submit():
          print("validated")
  
          difference = form.default_field_2.data - form.default_field.data
  
          if form.get_default_field_value_difference.data:
              flash( difference )
              form.dynamic_field.choices = [(i,i) for i in range(0,difference,5)]
  
              return render_template('mfe-dynamic-selectfield.html', form=form)
  
          if form.deduct_dynamic_field_value.data:
  
              if form.dynamic_field.data:
                  deducted = difference - form.dynamic_field.data
                  flash( deducted )
  
              else:
                  flash( "no dynamic field value chosen")           
  
              return render_template('mfe-dynamic-selectfield.html', form=form)
  
          else:
              flash( "nope" )
  
      return render_template('mfe-dynamic-selectfield.html', form=form)
  
  if __name__ == '__main__':
      app.run()
  </code></pre>
  
  <p>Bringing up the page works just fine, and immediately flashes "nope", as expected.</p>
  
  <p>Calculating the difference between the default set fields:
  -works every time as long they are both set to '0'
  -if either field is not set to '0', every other POST fails validation, and the time correctly calculates the difference and dynamically sets the last field.</p>
  
  <p>Trying to POST using the dynamically set field fails every time.</p>
  
  <p>Am I missing something very obvious here?</p>
  
  <p>I'm rendering using this:</p>
  
  <pre><code>{% block content %}
  &lt;form action="" method="post" name="mfe-dynamic-selectfield"&gt;
    {{ form.hidden_tag() }}
    &lt;table&gt;
    &lt;tr&gt;
        &lt;td&gt; Calculated value &lt;/td&gt;
        &lt;td&gt;
  
        {% with messages = get_flashed_messages() %}
            {% if messages %}
              &lt;ul class=flashes&gt;
              {% for message in messages %}
                &lt;li&gt;{{ message }}&lt;/li&gt;
              {% endfor %}
              &lt;/ul&gt;
            {% endif %}
         {% endwith %}
  
         &lt;/td&gt;
    &lt;/tr&gt;
  
    &lt;br&gt;
    &lt;tr&gt;
        &lt;td&gt;Default SelectField 1&lt;/td&gt;
        &lt;td&gt;Default SelectField 2&lt;/td&gt;
  
        &lt;td&gt;Dynamic Selectfield&lt;/td&gt;
    &lt;/tr&gt;
  
    &lt;br&gt;
  
    &lt;tr&gt;
        &lt;td&gt;{{ form.default_field }}&lt;/td&gt;
        &lt;td&gt;{{ form.default_field_2 }}&lt;/td&gt;
  
        &lt;td&gt;{{ form.dynamic_field }}&lt;/td&gt;
    &lt;/tr&gt;
  
    &lt;tr&gt;
        &lt;td&gt;{{ form.get_default_field_value_difference }}&lt;/td&gt;
        &lt;td&gt;{{ form.deduct_dynamic_field_value }}&lt;/td&gt;
    &lt;/tr&gt;      
  
    &lt;/table&gt;
  &lt;/form&gt;
  {% endblock %}
  </code></pre>