0
votes

I would like to create a datastore model for a book review webapp. This webapp would have two kinds - Book and Review:

class Book(ndb.Model):
    title = ndb.StringProperty()

class Review(ndb.Model):
    review = ndb.TextProperty()

The Reviewentities should be child entities to the parenting Book entities. Upon viewing a page of a Book entity, its list of Review child entities should also be queried and shown like this:

Review.query(ancestor=book.key).fetch()

The (simplified) handlers below would handle the creation of new entities:

class NewBook(BaseHandler):
    def get(self):
        self.render('new-book-form.html')

    def post(self):
        title = self.request.get('title')
        b = Book(title = title)
        b.put()

class NewReview(BaseHandler):
    def get(self):
        self.render('new-review-form.html')

    def post(self):
        book_key = self.get_book_key() # gets book entity's key
        review = self.request.get('review')
        r = Review(review = review) # Do I define this entity's parent here?
        r.put()

According to the documentation, a parent is assigned inside a child entity's key like this:

review = Review(parent=ndbKey(Book, book_id), review = review)

At which point inside the NewReview handler do I define this? I may be missing something very obvious.

1

1 Answers

0
votes

You pretty much have it right. Assuming your get_book_key method returns an actual ndb.Key instance, the line you've commented becomes:

r = Review(parent=book_key, review=review)