Rectangle 27 0

python Pre Populate a WTforms in flask, with data from a SQLAlchemy object?


@decorator_authorized_user  # This decorator should make sure the user is authorized, like @login_required from flask-login
def editprofile(nickname = None):
    # Prepare the form and user
    form = EditProfile()
    form_action = url_for('profile.editprofile')
    my_user = Users.get(...)  # get your user object or whatever you need
    if request.method == 'GET':
        form.username.data = my_user.username
        form.email.data = my_user.email
        # and on
    if form.validate_on_submit():
        # This section needs to be reworked.
        # You'll want to take the user object and set the appropriate attributes
        # to the appropriate values from the form.
        if form.username.data == nickname: 
            query = EditProfile(form.username.data,
                                form.email.data,
                                form.about.data,
                                form.website.data,
                                )
            print query #debug
            db.session.add(query)
            db.session.commit()
            flash('User Updated')
            print "added"
            return(url_for('profile.editprofile'))
    return render_template('profile/add.html', form=form,
                           form_action=form_action, title="Update Profile")

The easiest way I've found of doing this is to fill the form fields on a get request.

This sets up the function to return a prefilled form on a get request. You'll have to rework the section under form.validate_on_submit. Dirn's answer suggests a few of the right things to do.

Note
Rectangle 27 0

python Pre Populate a WTforms in flask, with data from a SQLAlchemy object?


query = EditProfile(form.username.data,
                             form.email.data,
                             form.about.data,
                             form.website.data,
                             )
          db.session.add(query)
form = EditProfile(obj=user)  # or whatever your object is called
form.populate_obj(user)  # or whatever your object is called

Based on your EditProfile form above and your code on GitHub, the fields have the same names in your user model and form.

Because your object was already loaded, you won't need to add it to the session. You can remove db.session.add(query) and just call db.session.commit().

I am still a bit confused. Once I am sending my user object in the form i need to edit the template also i think, otherwise how will the it associate the correct correct feild with models ? As data members in my user model and form class are having different names.

Instead, after validating the form, you can associate its values with the object.

It creates a new instance of your EditProfile form. You then try to add it to the session. The session wants models, not forms.

Why are they different? Also, can you show us the model?

Yeah, I have chnaged the names, and it works now. But What can be done to keep their names different.

Note