I am doing Udacity's Web Dev Course with Google Appengine and Python.
I would like to know how I could assign to a created entity, its own author.
For example, I have two ndb.Models kinds:
class User(ndb.Model):
username = ndb.StringProperty(required = True)
bio = ndb.TextProperty(required = True)
password = ndb.StringProperty(required = True)
email = ndb.StringProperty()
created = ndb.DateTimeProperty(auto_now_add = True)
class Blog(ndb.Model):
title = ndb.StringProperty(required = True)
body = ndb.TextProperty(required = True)
created = ndb.DateTimeProperty(auto_now_add = True)
When a Blog entity is created by a logged-in user, its own author (User entity) should also be identified with it.
Ultimately, I would like to display a blog's post with its author's information (for example, the author's bio)
How can this be achieved?
Your
Blog
class should include a property to store the key of the user who wrote it:You can then set this property when you create a Blog instance:
For optimization, if you know the logged in user's ndb.Key, and you don't need the user entity itself, you would pass that directly, instead of needing to fetch the user first.
In full:
You may get bonus points if you standardize the beginning of new_blog to a utility function.