简体   繁体   English

ReferenceProperty可能不会生成反向引用吗?

[英]Is there a reason that a ReferenceProperty might not generate a back-reference?

In my current project, I have two models, Version and Comment. 在当前项目中,我有两个模型,版本和注释。 There is a one-to-many relationship between the two; 两者之间存在一对多的关系。 each Version can have many Comment and the Comment model has a ReferenceProperty to record which Version it belongs to: 每个版本可以有多个注释,并且注释模型具有ReferenceProperty来记录其所属的版本:

class Comment(db.Model):
    version = db.ReferenceProperty(version.Version, collection_name="comments")

The problem is that instances of Version are not getting a comments property as I would expect. 问题在于,Version实例没有获得我期望的comment属性。 According to the docs , I should get an automagical property on each Version that is a query that returns all Comment instances that have their version set to the Version instance in question. 根据docs ,我应该在每个Version上都获得一个automagical属性,该属性是一个查询,该查询返回其版本设置为相关Version实例的所有Comment实例。 Doesn't seem to work for my code. 似乎不适用于我的代码。

I know that the ReferenceProperty is set correctly, because I can get the Comments with this query: 我知道ReferenceProperty设置正确,因为我可以通过以下查询获取评论:

        comments = comment.Comment.all().filter('version = ', self).order('-added_on').fetch(500)

but this does not: 但这不是:

        comments = self.comments.order('-added_on').fetch(500)

it crashes because self has no property comments. 它崩溃,因为self没有属性注释。

The complete code for the two models is included below. 这两个模型的完整代码如下。 Does anyone have any idea why the back-reference property is not given to my Verson instance? 有谁知道为什么不向我的Verson实例提供反向引用属性?

from version.py: 来自version.py:

from google.appengine.ext import db
import piece

class Version(db.Model):
    parent_piece = db.ReferenceProperty(piece.Piece, collection_name="versions")
    note = db.TextProperty()
    content = db.TextProperty()
    published_on = db.DateProperty(auto_now_add=True)

    def add_comment(self, member, content):
        import comment

        new_comment = None
        try:
            new_comment = comment.Comment()
            new_comment.version = self
            new_comment.author = member
            new_comment.author_moniker = member.moniker
            new_comment.content = content

            new_comment.put()
        except:
            # TODO: handle datastore exceptions here
            pass

        return new_comment

    def get_comments(self):
        import comment

        comments = None
        try:
            comments = comment.Comment.all().filter('version = ', self).order('-added_on').fetch(500)
        except:
            pass

from comment.py: 来自comment.py:

import version
import member
from google.appengine.ext import db

class Comment(db.Model):
    version = db.ReferenceProperty(version.Version, collection_name="comments")
    author = db.ReferenceProperty(member.Member)
    author_moniker = db.StringProperty()
    author_thumbnail_avatar_url = db.StringProperty()
    content = db.TextProperty()
    added_on = db.DateProperty(auto_now_add=True)

It looks like you are overwriting the automagical property called comments with your own property comments, in this line: 看起来您正在用以下自己的属性注释覆盖称为comments的自动属性:

comments = self.comments.order('-added_on').fetch(500)

What happens if you change the collection_name argument in your Comment model to be "comments_set", then change the above line to: 如果将Comment模型中的collection_name参数更改为“ comments_set”,然后将上述行更改为:

comments = self.comments_set.order('-added_on').fetch(500)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM