Filter by a Reference Property in Appnengine
<p>I am doing a blog in appengine. I want make a query to get the numbers of post by category. So I need filter by a Reference Property in appengine. Look my actual Code.</p>

<p>Those are my models :</p>

<pre><code>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 = [])
</code></pre>

<p>And this is my query :</p>

<pre><code>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
</code></pre>

<p>But It seems that filter("post.category =", cat.key()) is not the correct way to do this.</p>

<p>I haven't used <code>db</code> in a while, but I think something like this will work:</p>

<pre><code>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()
</code></pre>

python google-app-engine gae-datastore