How to link back to Google AppEngine?

I am trying to access the object that db.ReferenceProperty in the Google engine is bound to. Here's the model code:

class InquiryQuestion(db.Model):
    inquiry_ref = db.ReferenceProperty(reference_class=GiftInquiry, required=True, collection_name="inquiry_ref")

And I'm trying to access it as follows:

linkedObject = question.inquiry_ref

and then

linkedKey = linkedObject.key

but it does not work. Can anybody help?

+3
source share
2 answers

Your naming conventions are a bit confusing. query_ref is both your ReferenceProperty name and your back-reference collection name, so question.inquiry_ref gives you a KeyInsearch object, but question.inquiry_ref.inquiry_ref provides a query object filtered for InquiryQuestion objects.

, " " .

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()
+5

- . fetch() get() :

linkedObject = question.inquiry_ref.get()

. fetch(), , ref .

, , .

GiftInquiry, , query_ref, ( ), InquiryQuestion, query_ref, .

, InquiryQuestion GiftInquiry, query_ref, :

linkedObject = db.get(question.inquiry_ref)

query_ref - GiftInquiry, BackReference.

ReferenceProperty .

+3

Source: https://habr.com/ru/post/1748392/


All Articles