如何在Google AppEngine上进行反向引用?

jCu*_*uga 3 python google-app-engine web-applications

我正在尝试访问由Google应用引擎中的db.ReferenceProperty链接的对象.这是模型的代码:

class InquiryQuestion(db.Model):
    inquiry_ref = db.ReferenceProperty(reference_class=GiftInquiry, required=True, collection_name="inquiry_ref")
Run Code Online (Sandbox Code Playgroud)

我试图通过以下方式访问它:

linkedObject = question.inquiry_ref
Run Code Online (Sandbox Code Playgroud)

然后

linkedKey = linkedObject.key
Run Code Online (Sandbox Code Playgroud)

但它不起作用.有人可以帮忙吗?

Dre*_*ars 5

您的命名约定有点令人困惑.inquiry_ref既是您的ReferenceProperty名称又是您的反向引用集合名称,因此question.inquiry_ref为您提供了GiftInquiry Key对象,但是question.inquiry_ref.inquiry_ref为您提供了一个过滤到InquiryQuestion实体的Query对象.

假设我们有以下域模型,文章和评论之间存在一对多的关系.

class Article(db.Model):
  body = db.TextProperty()

class Comment(db.Model):
  article = db.ReferenceProperty(Article)
  body = db.TextProperty()

comment = Comment.all().get()

# The explicit reference from one comment to one article
# is represented by a Key object
article_key = comment.article

# which gets lazy-loaded to a Model instance by accessing a property
article_body = comment.article.body

# The implicit back-reference from one article to many comments
# is represented by a Query object
article_comments = comment.article.comment_set

# If the article only has one comment, this gives us a round trip
comment = comment.article.comment_set.all().get()
Run Code Online (Sandbox Code Playgroud)