Cameron Sima Cameron Sima - 20 days ago 8
Python Question

Google App Engine returning odd values from datastore

I'm trying to do a simple save string to datastore, redirect on that entity's key, then fetch it in the next handler. When I make the call in PDFHandler to fetch the string (a.name()) "w" is returned everytime, no matter what I enter.

class Pdf(db.Model):
name = db.StringProperty(required=True)

class InputHandler(Handler):
def get(self):
self.render('cert_form.html')

def post(self):
name = self.request.get('name')

if name:
a = Pdf(name=name)
a.put()
self.redirect('/key=%s' % a.key())
else:
error = "Please enter your full name."
self.render('cert_form.html')

class PDFHandler(Handler):
def get(self, id):
a = db.Key.from_path('Pdf', id)


self.response.write(a.name())

application = webapp2.WSGIApplication([
(r'/', InputHandler),
(r'/key=(.)+', PDFHandler),
], debug=True)

Answer

In your PDFHandler when you do

a = db.Key.from_path('Pdf', id)

What you are getting is the Key object not the entity yet.

To get the Pdf entity you can do the below

pdf = db.get(a)

Then you can access fields on pdf like pdf.name etc.

Comments