Reputation: 1282
I have the following Model in GAE's datastore:
class Blog(db.Model):
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
last_modified = db.DateTimeProperty(auto_now = True)
I create a new post with the following class:
class NewPostHandler(Handler):
def render_newpost(self, subject="", content="", error=""):
self.render("newpost.html", subject=subject, content=content, error=error)
def get(self):
self.render_newpost()
def post(self):
subject = self.request.get("subject")
content = self.request.get("content")
if subject and content:
a = Blog(parent = blog_key(), subject=subject, content=content)
a.put()
self.redirect("/blog/%s" %str(a.key().id()))
else:
error = "we need both a subject and some text!"
self.render_newpost(subject, content, error)
I use this piece of code to define a key:
def blog_key(name = 'default'):
return db.Key.from_path('blogs', name)
My problem is when trying to render it, the following using get_by_id works:
class PermalinkHandler(Handler):
def get(self, blog_id):
blog = Blog.get_by_id(int(blog_id), parent=None)
if not blog:
self.response.write('There is no blog whose id is %s' %blog_id)
return
self.render("permalink.html", blog = blog)
When I try to replace blog = Blog.get_by_id(int(blog_id), parent=None)
with
k = db.Key.from_path('Blog', int(blog_id), parent=blog_key())
blog = db.get(k)
It doesn't work, any idea why? Thx
Upvotes: 0
Views: 176
Reputation: 12986
The obvious question is are you supplying the parent in the working code.
def get(self, blog_id):
blog = Blog.get_by_id(int(blog_id), parent=None)
Looking at your code the parent is always None, and in your post handler you are not showing us how you create blog_key
Then in your non working code you have
k = db.Key.from_path('Blog', int(blog_id), parent=blog_key())
blog = db.get(k)
So my guess is the problem is due to inconsistent use of parent in your keys.
Upvotes: 1