librelist archives

« back to archive

Help/Example for Sqlalchemy-Wtforms Edit Form

Help/Example for Sqlalchemy-Wtforms Edit Form

From:
dorian i
Date:
2013-01-15 @ 22:30
Can anyone tell me of a repo or a snippet of code that has an Edit Form
based on a Sqlalchemy model? Because i'm trying in my code and i have
errors:

from flask_wtf import Form

class Company(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    website = db.Column(db.String(120), unique=True)
    #flys =

db.relationship('Fly',secondary=flycompany,backref=db.backref('companies',lazy='dynamic'))

    def __init__(self, name, website):
        self.name = name
        self.website = website

CompanyForm=model_form(Company,Form)


@app.route('/company/add/',methods=['GET','POST'])
def company_add():
    form = CompanyForm()
    if request.method == 'POST' and form.validate():
        print form.data
        company = Company(name=form.data.name,website=form.data.website)
        db.session.add(company)
        db.session.commit()
        flash('Congratulations on the new company')
        redirect(url_for('company_list'))
    return render_template('company_add.html',form = form)

The problem is form.validate_on_submit doesn't exist,when i POST the form
it has empty data. form.populate_object does not exist.

Thanks

Re: [flask] Help/Example for Sqlalchemy-Wtforms Edit Form

From:
David Van Duzer
Date:
2013-01-15 @ 22:57
Make sure your company_add.html template has:

    {{ form.csrf_token }}

somewhere in your <form> element.

On Tue, Jan 15, 2013 at 3:30 PM, dorian i <dorian.hoxha@gmail.com> wrote:
> Can anyone tell me of a repo or a snippet of code that has an Edit Form
> based on a Sqlalchemy model? Because i'm trying in my code and i have
> errors:
>
> from flask_wtf import Form
>
> class Company(db.Model):
>     id = db.Column(db.Integer, primary_key=True)
>     name = db.Column(db.String(80), unique=True)
>     website = db.Column(db.String(120), unique=True)
>     #flys =
> 
db.relationship('Fly',secondary=flycompany,backref=db.backref('companies',lazy='dynamic'))
>
>     def __init__(self, name, website):
>         self.name = name
>         self.website = website
>
> CompanyForm=model_form(Company,Form)
>
>
> @app.route('/company/add/',methods=['GET','POST'])
> def company_add():
>     form = CompanyForm()
>     if request.method == 'POST' and form.validate():
>         print form.data
>         company = Company(name=form.data.name,website=form.data.website)
>         db.session.add(company)
>         db.session.commit()
>         flash('Congratulations on the new company')
>         redirect(url_for('company_list'))
>     return render_template('company_add.html',form = form)
>
> The problem is form.validate_on_submit doesn't exist,when i POST the form it
> has empty data. form.populate_object does not exist.
>
> Thanks

Re: [flask] Help/Example for Sqlalchemy-Wtforms Edit Form

From:
dorian i
Date:
2013-01-16 @ 09:24
Already have. This is my current template for adding (which also doesnt
work):

    <form method="POST" action=".">
        {{ form.csrf_token }}
        {{ form.name.label }} {{ form.name(size=20) }}

        {{ form.website.label }}{{ form.website }}
        <input type="submit" value="Create the company">
    </form>

On Tue, Jan 15, 2013 at 11:57 PM, David Van Duzer <dvd@tennica.net> wrote:

> Make sure your company_add.html template has:
>
>     {{ form.csrf_token }}
>
> somewhere in your <form> element.
>
> On Tue, Jan 15, 2013 at 3:30 PM, dorian i <dorian.hoxha@gmail.com> wrote:
> > Can anyone tell me of a repo or a snippet of code that has an Edit Form
> > based on a Sqlalchemy model? Because i'm trying in my code and i have
> > errors:
> >
> > from flask_wtf import Form
> >
> > class Company(db.Model):
> >     id = db.Column(db.Integer, primary_key=True)
> >     name = db.Column(db.String(80), unique=True)
> >     website = db.Column(db.String(120), unique=True)
> >     #flys =
> >
> 
db.relationship('Fly',secondary=flycompany,backref=db.backref('companies',lazy='dynamic'))
> >
> >     def __init__(self, name, website):
> >         self.name = name
> >         self.website = website
> >
> > CompanyForm=model_form(Company,Form)
> >
> >
> > @app.route('/company/add/',methods=['GET','POST'])
> > def company_add():
> >     form = CompanyForm()
> >     if request.method == 'POST' and form.validate():
> >         print form.data
> >         company = Company(name=form.data.name,website=form.data.website)
> >         db.session.add(company)
> >         db.session.commit()
> >         flash('Congratulations on the new company')
> >         redirect(url_for('company_list'))
> >     return render_template('company_add.html',form = form)
> >
> > The problem is form.validate_on_submit doesn't exist,when i POST the
> form it
> > has empty data. form.populate_object does not exist.
> >
> > Thanks
>

Re: [flask] Help/Example for Sqlalchemy-Wtforms Edit Form

From:
anh le
Date:
2013-01-16 @ 10:12
Hi,

where did model_form come from, I think model_form is only for
extensions like appengine, django
for SQLAlchemy you have to use differently:
http://wtforms.simplecodes.com/docs/1.0.2/ext.html#module-wtforms.ext.sqlalchemy

Have a look at flask-admin which also support SQLAlchemy backend.
http://flask-admin.readthedocs.org/en/latest/


On Wed, Jan 16, 2013 at 4:24 PM, dorian i <dorian.hoxha@gmail.com> wrote:
> Already have. This is my current template for adding (which also doesnt
> work):
>
>     <form method="POST" action=".">
>         {{ form.csrf_token }}
>         {{ form.name.label }} {{ form.name(size=20) }}
>
>         {{ form.website.label }}{{ form.website }}
>         <input type="submit" value="Create the company">
>     </form>
>
> On Tue, Jan 15, 2013 at 11:57 PM, David Van Duzer <dvd@tennica.net> wrote:
>>
>> Make sure your company_add.html template has:
>>
>>     {{ form.csrf_token }}
>>
>> somewhere in your <form> element.
>>
>> On Tue, Jan 15, 2013 at 3:30 PM, dorian i <dorian.hoxha@gmail.com> wrote:
>> > Can anyone tell me of a repo or a snippet of code that has an Edit Form
>> > based on a Sqlalchemy model? Because i'm trying in my code and i have
>> > errors:
>> >
>> > from flask_wtf import Form
>> >
>> > class Company(db.Model):
>> >     id = db.Column(db.Integer, primary_key=True)
>> >     name = db.Column(db.String(80), unique=True)
>> >     website = db.Column(db.String(120), unique=True)
>> >     #flys =
>> >
>> > 
db.relationship('Fly',secondary=flycompany,backref=db.backref('companies',lazy='dynamic'))
>> >
>> >     def __init__(self, name, website):
>> >         self.name = name
>> >         self.website = website
>> >
>> > CompanyForm=model_form(Company,Form)
>> >
>> >
>> > @app.route('/company/add/',methods=['GET','POST'])
>> > def company_add():
>> >     form = CompanyForm()
>> >     if request.method == 'POST' and form.validate():
>> >         print form.data
>> >         company = Company(name=form.data.name,website=form.data.website)
>> >         db.session.add(company)
>> >         db.session.commit()
>> >         flash('Congratulations on the new company')
>> >         redirect(url_for('company_list'))
>> >     return render_template('company_add.html',form = form)
>> >
>> > The problem is form.validate_on_submit doesn't exist,when i POST the
>> > form it
>> > has empty data. form.populate_object does not exist.
>> >
>> > Thanks
>
>

Re: [flask] Help/Example for Sqlalchemy-Wtforms Edit Form

From:
dorian i
Date:
2013-01-16 @ 14:56
I used:

from wtforms.ext.sqlalchemy.orm import model_form


On Wed, Jan 16, 2013 at 11:12 AM, anh le <anhlh2@gmail.com> wrote:

> Hi,
>
> where did model_form come from, I think model_form is only for
> extensions like appengine, django
> for SQLAlchemy you have to use differently:
>
> http://wtforms.simplecodes.com/docs/1.0.2/ext.html#module-wtforms.ext.sqlalchemy
>
> Have a look at flask-admin which also support SQLAlchemy backend.
> http://flask-admin.readthedocs.org/en/latest/
>
>
> On Wed, Jan 16, 2013 at 4:24 PM, dorian i <dorian.hoxha@gmail.com> wrote:
> > Already have. This is my current template for adding (which also doesnt
> > work):
> >
> >     <form method="POST" action=".">
> >         {{ form.csrf_token }}
> >         {{ form.name.label }} {{ form.name(size=20) }}
> >
> >         {{ form.website.label }}{{ form.website }}
> >         <input type="submit" value="Create the company">
> >     </form>
> >
> > On Tue, Jan 15, 2013 at 11:57 PM, David Van Duzer <dvd@tennica.net>
> wrote:
> >>
> >> Make sure your company_add.html template has:
> >>
> >>     {{ form.csrf_token }}
> >>
> >> somewhere in your <form> element.
> >>
> >> On Tue, Jan 15, 2013 at 3:30 PM, dorian i <dorian.hoxha@gmail.com>
> wrote:
> >> > Can anyone tell me of a repo or a snippet of code that has an Edit
> Form
> >> > based on a Sqlalchemy model? Because i'm trying in my code and i have
> >> > errors:
> >> >
> >> > from flask_wtf import Form
> >> >
> >> > class Company(db.Model):
> >> >     id = db.Column(db.Integer, primary_key=True)
> >> >     name = db.Column(db.String(80), unique=True)
> >> >     website = db.Column(db.String(120), unique=True)
> >> >     #flys =
> >> >
> >> >
> 
db.relationship('Fly',secondary=flycompany,backref=db.backref('companies',lazy='dynamic'))
> >> >
> >> >     def __init__(self, name, website):
> >> >         self.name = name
> >> >         self.website = website
> >> >
> >> > CompanyForm=model_form(Company,Form)
> >> >
> >> >
> >> > @app.route('/company/add/',methods=['GET','POST'])
> >> > def company_add():
> >> >     form = CompanyForm()
> >> >     if request.method == 'POST' and form.validate():
> >> >         print form.data
> >> >         company = Company(name=form.data.name
> ,website=form.data.website)
> >> >         db.session.add(company)
> >> >         db.session.commit()
> >> >         flash('Congratulations on the new company')
> >> >         redirect(url_for('company_list'))
> >> >     return render_template('company_add.html',form = form)
> >> >
> >> > The problem is form.validate_on_submit doesn't exist,when i POST the
> >> > form it
> >> > has empty data. form.populate_object does not exist.
> >> >
> >> > Thanks
> >
> >
>

Re: [flask] Help/Example for Sqlalchemy-Wtforms Edit Form

From:
anh le
Date:
2013-01-17 @ 02:54
Hi Dorian,

On Wed, Jan 16, 2013 at 9:56 PM, dorian i <dorian.hoxha@gmail.com> wrote:
> I used:
>
> from wtforms.ext.sqlalchemy.orm import model_form

You're right, I was confused by the docs and totally wrong about the
wtform sqlalchemy extension.
I reproduced your example and missed the validate_on_submit too.

while using form.validate, the form.data won't be empty and I can persist
the data when I add some modifies:

1. modify company_add:
form = CompanyForm(request.form)
...
company = Comprany(name=form.data['name'],website=form.data['website'])

but it still broken by validate then I add 2nd change:

2. disable validate in model fields:
disable unique constrain of 'name' and 'website' because I found that
from the debug web console
wtform validate the unique fields  and get_session return Form (not
the expected session) so
'query' attribute not found.

That my findings so far,


>
>
>
> On Wed, Jan 16, 2013 at 11:12 AM, anh le <anhlh2@gmail.com> wrote:
>>
>> Hi,
>>
>> where did model_form come from, I think model_form is only for
>> extensions like appengine, django
>> for SQLAlchemy you have to use differently:
>>
>> 
http://wtforms.simplecodes.com/docs/1.0.2/ext.html#module-wtforms.ext.sqlalchemy
>>
>> Have a look at flask-admin which also support SQLAlchemy backend.
>> http://flask-admin.readthedocs.org/en/latest/
>>
>>
>> On Wed, Jan 16, 2013 at 4:24 PM, dorian i <dorian.hoxha@gmail.com> wrote:
>> > Already have. This is my current template for adding (which also doesnt
>> > work):
>> >
>> >     <form method="POST" action=".">
>> >         {{ form.csrf_token }}
>> >         {{ form.name.label }} {{ form.name(size=20) }}
>> >
>> >         {{ form.website.label }}{{ form.website }}
>> >         <input type="submit" value="Create the company">
>> >     </form>
>> >
>> > On Tue, Jan 15, 2013 at 11:57 PM, David Van Duzer <dvd@tennica.net>
>> > wrote:
>> >>
>> >> Make sure your company_add.html template has:
>> >>
>> >>     {{ form.csrf_token }}
>> >>
>> >> somewhere in your <form> element.
>> >>
>> >> On Tue, Jan 15, 2013 at 3:30 PM, dorian i <dorian.hoxha@gmail.com>
>> >> wrote:
>> >> > Can anyone tell me of a repo or a snippet of code that has an Edit
>> >> > Form
>> >> > based on a Sqlalchemy model? Because i'm trying in my code and i have
>> >> > errors:
>> >> >
>> >> > from flask_wtf import Form
>> >> >
>> >> > class Company(db.Model):
>> >> >     id = db.Column(db.Integer, primary_key=True)
>> >> >     name = db.Column(db.String(80), unique=True)
>> >> >     website = db.Column(db.String(120), unique=True)
>> >> >     #flys =
>> >> >
>> >> >
>> >> > 
db.relationship('Fly',secondary=flycompany,backref=db.backref('companies',lazy='dynamic'))
>> >> >
>> >> >     def __init__(self, name, website):
>> >> >         self.name = name
>> >> >         self.website = website
>> >> >
>> >> > CompanyForm=model_form(Company,Form)
>> >> >
>> >> >
>> >> > @app.route('/company/add/',methods=['GET','POST'])
>> >> > def company_add():
>> >> >     form = CompanyForm()
>> >> >     if request.method == 'POST' and form.validate():
>> >> >         print form.data
>> >> >         company =
>> >> > Company(name=form.data.name,website=form.data.website)
>> >> >         db.session.add(company)
>> >> >         db.session.commit()
>> >> >         flash('Congratulations on the new company')
>> >> >         redirect(url_for('company_list'))
>> >> >     return render_template('company_add.html',form = form)
>> >> >
>> >> > The problem is form.validate_on_submit doesn't exist,when i POST the
>> >> > form it
>> >> > has empty data. form.populate_object does not exist.
>> >> >
>> >> > Thanks
>> >
>> >
>
>

Re: [flask] Help/Example for Sqlalchemy-Wtforms Edit Form

From:
anh le
Date:
2013-01-17 @ 05:00
Hi Dorian,

From the debug console I found the reason. You should change to

CompanyForm = model_form(Company, db.session, Form)

2nd, 3rd params should be the db session then Form now validate_on_submit work

I post the working code here : http://pastebin.com/FZ39guEW


On Thu, Jan 17, 2013 at 9:54 AM, anh le <anhlh2@gmail.com> wrote:
> Hi Dorian,
>
> On Wed, Jan 16, 2013 at 9:56 PM, dorian i <dorian.hoxha@gmail.com> wrote:
>> I used:
>>
>> from wtforms.ext.sqlalchemy.orm import model_form
>
> You're right, I was confused by the docs and totally wrong about the
> wtform sqlalchemy extension.
> I reproduced your example and missed the validate_on_submit too.
>
> while using form.validate, the form.data won't be empty and I can persist
> the data when I add some modifies:
>
> 1. modify company_add:
> form = CompanyForm(request.form)
> ...
> company = Comprany(name=form.data['name'],website=form.data['website'])
>
> but it still broken by validate then I add 2nd change:
>
> 2. disable validate in model fields:
> disable unique constrain of 'name' and 'website' because I found that
> from the debug web console
> wtform validate the unique fields  and get_session return Form (not
> the expected session) so
> 'query' attribute not found.
>
> That my findings so far,
>
>
>>
>>
>>
>> On Wed, Jan 16, 2013 at 11:12 AM, anh le <anhlh2@gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> where did model_form come from, I think model_form is only for
>>> extensions like appengine, django
>>> for SQLAlchemy you have to use differently:
>>>
>>> 
http://wtforms.simplecodes.com/docs/1.0.2/ext.html#module-wtforms.ext.sqlalchemy
>>>
>>> Have a look at flask-admin which also support SQLAlchemy backend.
>>> http://flask-admin.readthedocs.org/en/latest/
>>>
>>>
>>> On Wed, Jan 16, 2013 at 4:24 PM, dorian i <dorian.hoxha@gmail.com> wrote:
>>> > Already have. This is my current template for adding (which also doesnt
>>> > work):
>>> >
>>> >     <form method="POST" action=".">
>>> >         {{ form.csrf_token }}
>>> >         {{ form.name.label }} {{ form.name(size=20) }}
>>> >
>>> >         {{ form.website.label }}{{ form.website }}
>>> >         <input type="submit" value="Create the company">
>>> >     </form>
>>> >
>>> > On Tue, Jan 15, 2013 at 11:57 PM, David Van Duzer <dvd@tennica.net>
>>> > wrote:
>>> >>
>>> >> Make sure your company_add.html template has:
>>> >>
>>> >>     {{ form.csrf_token }}
>>> >>
>>> >> somewhere in your <form> element.
>>> >>
>>> >> On Tue, Jan 15, 2013 at 3:30 PM, dorian i <dorian.hoxha@gmail.com>
>>> >> wrote:
>>> >> > Can anyone tell me of a repo or a snippet of code that has an Edit
>>> >> > Form
>>> >> > based on a Sqlalchemy model? Because i'm trying in my code and i have
>>> >> > errors:
>>> >> >
>>> >> > from flask_wtf import Form
>>> >> >
>>> >> > class Company(db.Model):
>>> >> >     id = db.Column(db.Integer, primary_key=True)
>>> >> >     name = db.Column(db.String(80), unique=True)
>>> >> >     website = db.Column(db.String(120), unique=True)
>>> >> >     #flys =
>>> >> >
>>> >> >
>>> >> > 
db.relationship('Fly',secondary=flycompany,backref=db.backref('companies',lazy='dynamic'))
>>> >> >
>>> >> >     def __init__(self, name, website):
>>> >> >         self.name = name
>>> >> >         self.website = website
>>> >> >
>>> >> > CompanyForm=model_form(Company,Form)
>>> >> >
>>> >> >
>>> >> > @app.route('/company/add/',methods=['GET','POST'])
>>> >> > def company_add():
>>> >> >     form = CompanyForm()
>>> >> >     if request.method == 'POST' and form.validate():
>>> >> >         print form.data
>>> >> >         company =
>>> >> > Company(name=form.data.name,website=form.data.website)
>>> >> >         db.session.add(company)
>>> >> >         db.session.commit()
>>> >> >         flash('Congratulations on the new company')
>>> >> >         redirect(url_for('company_list'))
>>> >> >     return render_template('company_add.html',form = form)
>>> >> >
>>> >> > The problem is form.validate_on_submit doesn't exist,when i POST the
>>> >> > form it
>>> >> > has empty data. form.populate_object does not exist.
>>> >> >
>>> >> > Thanks
>>> >
>>> >
>>
>>

Re: [flask] Help/Example for Sqlalchemy-Wtforms Edit Form

From:
dorian i
Date:
2013-01-17 @ 09:17
Thank you very much. I have already seen the order of the parameters
required but it didn't cross my mind that the db.session couldn't be caught
automagically because the wtforms doesn't know that we are using it with
flask. Again thanks.

On Thu, Jan 17, 2013 at 6:00 AM, anh le <anhlh2@gmail.com> wrote:

> Hi Dorian,
>
> >From the debug console I found the reason. You should change to
>
> CompanyForm = model_form(Company, db.session, Form)
>
> 2nd, 3rd params should be the db session then Form now validate_on_submit
> work
>
> I post the working code here : http://pastebin.com/FZ39guEW
>
>
> On Thu, Jan 17, 2013 at 9:54 AM, anh le <anhlh2@gmail.com> wrote:
> > Hi Dorian,
> >
> > On Wed, Jan 16, 2013 at 9:56 PM, dorian i <dorian.hoxha@gmail.com>
> wrote:
> >> I used:
> >>
> >> from wtforms.ext.sqlalchemy.orm import model_form
> >
> > You're right, I was confused by the docs and totally wrong about the
> > wtform sqlalchemy extension.
> > I reproduced your example and missed the validate_on_submit too.
> >
> > while using form.validate, the form.data won't be empty and I can persist
> > the data when I add some modifies:
> >
> > 1. modify company_add:
> > form = CompanyForm(request.form)
> > ...
> > company = Comprany(name=form.data['name'],website=form.data['website'])
> >
> > but it still broken by validate then I add 2nd change:
> >
> > 2. disable validate in model fields:
> > disable unique constrain of 'name' and 'website' because I found that
> > from the debug web console
> > wtform validate the unique fields  and get_session return Form (not
> > the expected session) so
> > 'query' attribute not found.
> >
> > That my findings so far,
> >
> >
> >>
> >>
> >>
> >> On Wed, Jan 16, 2013 at 11:12 AM, anh le <anhlh2@gmail.com> wrote:
> >>>
> >>> Hi,
> >>>
> >>> where did model_form come from, I think model_form is only for
> >>> extensions like appengine, django
> >>> for SQLAlchemy you have to use differently:
> >>>
> >>>
> http://wtforms.simplecodes.com/docs/1.0.2/ext.html#module-wtforms.ext.sqlalchemy
> >>>
> >>> Have a look at flask-admin which also support SQLAlchemy backend.
> >>> http://flask-admin.readthedocs.org/en/latest/
> >>>
> >>>
> >>> On Wed, Jan 16, 2013 at 4:24 PM, dorian i <dorian.hoxha@gmail.com>
> wrote:
> >>> > Already have. This is my current template for adding (which also
> doesnt
> >>> > work):
> >>> >
> >>> >     <form method="POST" action=".">
> >>> >         {{ form.csrf_token }}
> >>> >         {{ form.name.label }} {{ form.name(size=20) }}
> >>> >
> >>> >         {{ form.website.label }}{{ form.website }}
> >>> >         <input type="submit" value="Create the company">
> >>> >     </form>
> >>> >
> >>> > On Tue, Jan 15, 2013 at 11:57 PM, David Van Duzer <dvd@tennica.net>
> >>> > wrote:
> >>> >>
> >>> >> Make sure your company_add.html template has:
> >>> >>
> >>> >>     {{ form.csrf_token }}
> >>> >>
> >>> >> somewhere in your <form> element.
> >>> >>
> >>> >> On Tue, Jan 15, 2013 at 3:30 PM, dorian i <dorian.hoxha@gmail.com>
> >>> >> wrote:
> >>> >> > Can anyone tell me of a repo or a snippet of code that has an Edit
> >>> >> > Form
> >>> >> > based on a Sqlalchemy model? Because i'm trying in my code and i
> have
> >>> >> > errors:
> >>> >> >
> >>> >> > from flask_wtf import Form
> >>> >> >
> >>> >> > class Company(db.Model):
> >>> >> >     id = db.Column(db.Integer, primary_key=True)
> >>> >> >     name = db.Column(db.String(80), unique=True)
> >>> >> >     website = db.Column(db.String(120), unique=True)
> >>> >> >     #flys =
> >>> >> >
> >>> >> >
> >>> >> >
> 
db.relationship('Fly',secondary=flycompany,backref=db.backref('companies',lazy='dynamic'))
> >>> >> >
> >>> >> >     def __init__(self, name, website):
> >>> >> >         self.name = name
> >>> >> >         self.website = website
> >>> >> >
> >>> >> > CompanyForm=model_form(Company,Form)
> >>> >> >
> >>> >> >
> >>> >> > @app.route('/company/add/',methods=['GET','POST'])
> >>> >> > def company_add():
> >>> >> >     form = CompanyForm()
> >>> >> >     if request.method == 'POST' and form.validate():
> >>> >> >         print form.data
> >>> >> >         company =
> >>> >> > Company(name=form.data.name,website=form.data.website)
> >>> >> >         db.session.add(company)
> >>> >> >         db.session.commit()
> >>> >> >         flash('Congratulations on the new company')
> >>> >> >         redirect(url_for('company_list'))
> >>> >> >     return render_template('company_add.html',form = form)
> >>> >> >
> >>> >> > The problem is form.validate_on_submit doesn't exist,when i POST
> the
> >>> >> > form it
> >>> >> > has empty data. form.populate_object does not exist.
> >>> >> >
> >>> >> > Thanks
> >>> >
> >>> >
> >>
> >>
>