Reputation: 415
Recently I read the 'Core Python' book chapter about App Engine and implemented the blog sample available through the explanation. I'm trying to expand this sample to allow comments in the blog posts, but I'm facing some difficulties.
I have two Models:
BlogPost (tag, text, timestamp) - it is the table which stores the tag, the text and the time that post was created
Comment (blog_post, text) - blog_post is a ReferenceProperty to BlogPost model to assign the comments with its related blog posts.
My difficulty is in retrieve the BlogPost key to assign it correctly with the comment that was entered by the user. I´m thinking that i should perform a query for the posts, take its keys and put it on the new comments created (if it was created, of course) by the user. But in this moment I don't understand (in a clearly way) how I should code these steps.
In other words, i need to do something related to the 'one to many' explanation available here - https://developers.google.com/appengine/articles/modeling. But i need to do according with my implementation, generating the instance models with the user inputs in my html forms.
Below, I let the codes that I'm working now and thanks for any help.
//app.yaml
application: src
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
and
//main.py
import webapp2
from google.appengine.ext import db
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write('''
<h1>The Blog Posts</h1>
<form action="/post" method=post>
Title:
<br><input type=text name=title>
<br>Body:
<br><textarea name=body rows=3 cols=60></textarea>
<br><input type=submit value="Post">
</form>
<hr>
''')
#posts = db.GqlQuery("SELECT * FROM BlogEntry")
posts = BlogPost.all().order('-timestamp').fetch(10)
for post in posts:
self.response.out.write('''<hr>
<strong>%s</strong><br>%s
<blockquote>%s</blockquote>''' % (
post.title, post.timestamp, post.body)
)
class BlogEntry(webapp2.RequestHandler):
def post(self):
post = BlogPost()
post.title = self.request.get('title')
post.body = self.request.get('body')
post.put()
self.redirect('/')
#model
class BlogPost(db.Model):
title = db.StringProperty()
body = db.TextProperty()
timestamp = db.DateTimeProperty(auto_now_add=True)
app = webapp2.WSGIApplication([
('/', MainHandler),
('/post', BlogEntry)
], debug=True)
Upvotes: 0
Views: 686
Reputation: 11706
In NDB you can use a repeated property or a repeated structured propert if you have additional field in your comment. A repeated property behaves like a Python list where you can put all the comments of a blog post. And you only need a single read.
Upvotes: 0
Reputation: 169424
One design is to have a comment form for each blog post. Then you can, for example, include the key of the BlogPost
as a hidden field in the form. To lessen clutter on your landing page you may wish to have a separate page for each BlogPost
within which you allow the user to submit their comment.
There is an example of this design here: https://bitbucket.org/abernier/yab/src The relevant pieces of code are PostHandler
in /views.py
and the accompanying markup is in /templates/view.html
.
Upvotes: 1