has no attribute validate_on_submit: How to use wtforms-alchemy's ModelForm in a flask handler / view
<p>I'm trying to switch <code>from wtforms.ext.sqlalchemy.orm import model_form</code> to using wtforms_alchemy ModelForm: </p>

<pre><code>from wtforms_alchemy import ModelForm 
</code></pre>

<p>I'm failing miserably... and I've been unsuccessful searching for a working example of that uses wtforms_alchemy which shows both the handler and the model.</p>

<p>Using model_form works for me: Here's my working code using model forms:</p>

<p><strong>The model script (python):</strong></p>

<pre><code>from app import db

class Test(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    test1 = db.Column(db.String())
    test2 = db.Column(db.String())
</code></pre>

<p><strong>The handler script (python, flask, model_form):</strong></p>

<pre><code>from flask.ext.wtf import Form
from wtforms.ext.sqlalchemy.orm import model_form
from wtforms import validators

from app import app
from app import db

from app.models import *

@app.route('/', methods=['GET', 'POST'])
@login_required
def index():
    TestForm = model_form(Test, base_class = Form, db_session=db.session, field_args = {
        'test1' : {
            'validators' : [validators.Length(max=1)]
        }
    })
    form = TestForm()

    if form.validate_on_submit():
        flash("success")
        new_test = Test()
        form.populate_obj(new_test)
        db.session.add(new_test)
        db.session.commit()        
        return redirect(url_for("index"))
    return render_template("main.html", form=form)
</code></pre>

<p>The template (uses mako):</p>

<pre><code>&lt;form class="form" action="/" method="POST"&gt;
    % for f in form:
        %if f.widget.input_type != 'hidden':
            &lt;dt&gt;${ f.label }&lt;/dt&gt;
        % endif

      &lt;dd&gt; 
        ${f}
        % if f.errors:
            &lt;div class="flashes alert alert-danger"&gt;
                &lt;a href="#" class="close" data-dismiss="alert" aria-label="close"&gt;&amp;times;&lt;/a&gt;

                % for e in f.errors:
                    &lt;p&gt;${e}&lt;/p&gt;
                % endfor
            &lt;/div&gt;
        % endif
      &lt;/dd&gt;
    % endfor
    &lt;input type="submit" value="go"&gt;
 &lt;/form&gt;
</code></pre>

<p>When I try to change my handler script page over to using ModelForm with this code (below) I get a <strong>'TestForm' object has no attribute 'validate_on_submit'</strong> error</p>

<p><strong>The handler script (python, flask, ModelForm):</strong></p>

<pre><code>from flask.ext.wtf import Form
from wtforms_alchemy import ModelForm
from wtforms import validators

from app import app
from app import db

from app.models import *

class TestForm(ModelForm):
    class Meta:
        model = Test

@app.route('/', methods=['GET', 'POST'])
@login_required
def index():
    form = TestForm()

    if form.validate_on_submit():
        flash("success")
        new_test = Test()
        form.populate_obj(new_test)
        db.session.add(new_test)
        db.session.commit()        
        return redirect(url_for("index"))
    return render_template("main.html", form=form)
</code></pre>

<p>What am I missing?</p>

<p>Nevermind.</p>

<p>Although I had tried using</p>

<pre><code>from wtforms_alchemy import model_form_factory
</code></pre>

<p>prior to this post, it decided to work when I put it back, but after the ModelForm import instead of before it.</p>

<p>Whatever.</p>

python sqlalchemy flask-sqlalchemy wtforms