Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate my WTForm variables?

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... )
like image 600
Niklas Rosencrantz Avatar asked Nov 14 '11 21:11

Niklas Rosencrantz


1 Answers

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

like image 80
Thomas Johansson Avatar answered Sep 18 '22 15:09

Thomas Johansson