Automatically populating class with request parameters

21 views
Skip to first unread message

Marcel Overdijk

unread,
Feb 18, 2013, 4:22:45 AM2/18/13
to webapp2
Is there an easy way with webapp2 to convert multiple request
parameters to a class instance?
E.g. I have a Person class containing firstname, lastname, dob, etc.
In a handler I receive similar fields which I would like to create a
Person instance from.
Is it possible to do this without retrieving all request parameters
manually?

alex

unread,
Feb 18, 2013, 5:24:36 AM2/18/13
to web...@googlegroups.com
If your Person is an NDB model, e.g.

class Person(ndb.Model):
firstname = ndb.StringProperty()
lastname = ndb.StringProperty()
...

Then you could do this in your request handler:

def post(self):
person = Person(**self.request.params)
person.put()

But, I would advise againts this, or do at least some pre-filtering
sanitizing request.params before instantiating Person.
> --
> You received this message because you are subscribed to the Google Groups "webapp2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to webapp2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

John Goodyear

unread,
Feb 18, 2013, 5:26:21 AM2/18/13
to web...@googlegroups.com
Consider something like WTForms: http://wtforms.simplecodes.com/docs/1.0.3/


Marcel Overdijk

unread,
Feb 20, 2013, 2:46:06 PM2/20/13
to web...@googlegroups.com
Thanks you I will look into both.
Reply all
Reply to author
Forward
0 new messages