Skip to content
Advertisement

has no attribute validate_on_submit: How to use wtforms-alchemy’s ModelForm in a flask handler / view

I’m trying to switch from wtforms.ext.sqlalchemy.orm import model_form to using wtforms_alchemy ModelForm:

from wtforms_alchemy import ModelForm 

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.

Using model_form works for me: Here’s my working code using model forms:

The model script (python):

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())

The handler script (python, flask, model_form):

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)

The template (uses mako):

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

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

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

When I try to change my handler script page over to using ModelForm with this code (below) I get a ‘TestForm’ object has no attribute ‘validate_on_submit’ error

The handler script (python, flask, ModelForm):

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)

What am I missing?

Advertisement

Answer

Nevermind.

Although I had tried using

from wtforms_alchemy import model_form_factory

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

Whatever.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement