How to populate my WTForm variables?

10,634

You need to pass your object via the form's second argument, "obj":

editAdForm = AdForm(obj=ad)

Outlined in the documentation crash course here: http://wtforms.simplecodes.com/docs/dev/crash_course.html#editing-existing-objects

Share:
10,634
Niklas Rosencrantz
Author by

Niklas Rosencrantz

I'm as simple as possible but not any simpler.

Updated on June 08, 2022

Comments

  • Niklas Rosencrantz
    Niklas Rosencrantz almost 2 years

    I'm enabling a function that can edit an entity. I want to populate the form with the variables from the datastore. How can I do it? My code doesn't populate the form:

    if self.request.get('id'):
      id = int(self.request.get('id'))
      ad = Ad.get(db.Key.from_path('Ad', id))
      im = ad.matched_images
      editAdForm = AdForm(ad)
      if str(users.get_current_user()) == str(ad.user) or users.is_current_user_admin():                    
        self.render_jinja('edit', form_url=blobstore.create_upload_url('/addimage'),
                            admin=users.is_current_user_admin(),
                            user_url= (users.create_logout_url('/'
                                    ) if users.get_current_user() else users.create_login_url(self.request.uri)),
                            user= users.get_current_user(),
                            ad= ad,
                            form = editAdForm)
    

    Instead I see this error message:

    formdata should be a multidict-type wrapper that supports the 'getlist' method:

    Update

    The workaround is to populate the form like this but I wonder if this really is the recommended way?

    editAForm = AForm(name=article.name, title=article.title, text=article.text... )
    
  • Niklas Rosencrantz
    Niklas Rosencrantz over 12 years
    Thank you Thomas for the solution.
  • swdev
    swdev over 10 years
    Great! I was confuse of finding the end point of form.populate_obj() ;)