fedorSmirnov fedorSmirnov - 1 year ago 97
Python Question

Google app engine: creating datastore entity from the post data

I am working on implementing a small online survey using the google app engine. I've already made the survey itself. What I need now is the possibility to store all the inputs from a person participating in this survey in the database.

The straight forward way to achieve this would be to create an entity with a property for each of the inputs and to create such an entity each time a person submits a filled out survey.

As I have about 30 inputs in my survey, I would like to know whether there is a more elegant or at least a less tedious way to turn the data in the post request into a datastore entity.

Thank you.

Answer Source

WTForms has a GAE extension that can be used to generate forms from models and models from forms.

With WTForms, your form field HTML can be generated for you, but we let you customize it in your templates. This allows you to maintain separation of code and presentation, and keep those messy parameters out of your python code. Because we strive for loose coupling, you should be able to do that in any templating engine you like, as well.


wtforms.ext.appengine.db.model_form(model, base_class=Form, only=None, exclude=None, field_args=None, converter=None)

Creates and returns a dynamic wtforms.Form class for a given db.Model class. The form class can be used as it is or serve as a base for extended form classes, which can then mix non-model related fields, subforms with other model forms, among other possibilities.

Creating a model based on a form involves creating a form object then passing the POST data to it.


formdata – Used to pass data coming from the enduser, usually request.POST or equivalent. formdata should be some sort of request-data wrapper which can get multiple parameters from the form input, and values are unicode strings, e.g. a Werkzeug/Django/WebOb MultiDict

And there is a similar question here: WTForms error:TypeError: formdata should be a multidict-type wrapper


def post(self):
   form = model_form(Ticket)
   form_object = form(formdata=self.request.POST)

from some random example I just found.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download