我正在做一个关于appengine的博客。我想进行查询以获取按类别发布的数量。所以我需要通过appengine中的Reference属性进行过滤。看看我的实际代码。
这些是我的模特:
class Comment(db.Model) :
user = db.ReferenceProperty(User)
post = db.ReferenceProperty(Blog)
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
date = db.DateProperty(auto_now_add = True)
last_modified = db.DateProperty()
status = db.BooleanProperty(default = True)
class Category(db.Model):
name = db.StringProperty()
date = db.DateProperty(auto_now_add=True)
class Blog(db.Model) :
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
date = db.DateProperty(auto_now_add = True)
category = db.ReferenceProperty(Category)
user = db.ReferenceProperty(User)
last_modified = db.DateProperty(auto_now = True)
status = db.BooleanProperty()
likes = db.IntegerProperty(default = 0)
users_liked = db.ListProperty(db.Key, default = [])
dislikes = db.IntegerProperty(default = 0)
users_disliked = db.ListProperty(db.Key, default = [])
这是我的疑问:
def numcomments_all_category() :
dic = {}
category = get_category()
for cat in category :
dic[cat.key().id()] = Comment.all().filter("post.category =", cat.key()).ancestor(ancestor_key).count()
return dic
但似乎过滤器(“post.category =”,cat.key())不是正确的方法。
答案 0 :(得分:0)
我暂时没有使用db
,但我认为这样的事情会起作用:
count = 0
# Get all blogs of the desired category
blogs = Blog.all().filter("category =", cat.key())
for blog in blogs:
# For each blog, count all the comments.
count += Comment.all().filter("post =", blog.key()).count()