I am trying to implement an 'Edit' function for entities in a google app engine datastore. So if a key is specified for an datastore entity I would like to fill a form with values associated with this key. If no key is specified, then these values will be pulled from the previous form post attempt. While I am able to loop through the self.request fields with self.request.arguments(), I do not know how to do this with the datastore (db.get(key)) object.
So please let me know if this is possible, or even if I am way off with my gae form processing.
class ItemsPage(webapp2.RequestHandler):
def __init__(self, *args, **kwargs):
super(ItemsPage, self).__init__(*args, **kwargs)
self.formerrors = dict()
self.formdata = dict()
def get(self):
if self.request.get('key'):
# Key specified, get field values from db store
key = self.request.get('key')
item = db.get(key)
for field in item.arguments(): # THIS DOES NOT WORK
self.formdata[field] = item.field
else:
# No key specified, use the self.request values
for field in self.request.arguments(): # THIS DOES WORK
self.formdata[field] = self.request.get(field)
# render the template with the formdata
template_values = {
'formerrors': self.formerrors,
'formdata': self.formdata # Use formdata to populate form fields
}
template = JINJA_ENVIRONMENT.get_template(
'templates/items.html'
)
self.response.write(template.render(template_values))
def post(self):
if self.validateSortForm():
# Form input is valid, do some processing and then put into datastore
item = itemDatstore()
item.put()
self.redirect('/')
else():
# Form input was not valid, redisplay the form
self.get()
Thank you
Thanks for the help marcadian, that put me on the right path. As the docs say
Model.properties ()
Returns a dictionary of all of the properties defined for this model class.
So I call properties() on a db.Model object from google.appengine.ext and get a dict of all the properties that I can iterate through.
if self.request.get('key'):
# Key specified, get field values from db store
key = self.request.get('key')
sort = db.get(key)
for field in ItemDB.properties():
self.formdata[field] = getattr(sort, field)