Python google文档中的ReferenceProperty示例对我来说太简洁了

Python google文档中的ReferenceProperty示例对我来说太简洁了,python,google-app-engine,referenceproperty,Python,Google App Engine,Referenceproperty,我试图重现这些例子。我编造了几个例子来试图理解它们。如果您能给我一些指导,让我的例子发挥作用,我将不胜感激。你可能还记得下面的引语,我认为这就是这些例子试图描述的内容 ReferenceProperty自动引用和取消引用模型 实例作为特性值:可以将模型实例指定给 直接引用属性,并将使用其键 以下更新 在我的问题中,我希望对下面复制的文档中的4行代码进行澄清和示例 story = db.get(story_key) author_name = story.author.name author =

我试图重现这些例子。我编造了几个例子来试图理解它们。如果您能给我一些指导,让我的例子发挥作用,我将不胜感激。你可能还记得下面的引语,我认为这就是这些例子试图描述的内容

ReferenceProperty自动引用和取消引用模型 实例作为特性值:可以将模型实例指定给 直接引用属性,并将使用其键

以下更新

在我的问题中,我希望对下面复制的文档中的4行代码进行澄清和示例

story = db.get(story_key)
author_name = story.author.name

author = db.get(author_key)
stories_by_author = author.story_set.get()
但这段代码与ndb模型无关,所有回复者似乎都坚持我应该使用ndb而不是db。由于我不知道如何为ndb编码,也无法重现db结果,因此我无法从文档中想象
ReferenceProperty
或现在的
KeyProperty
是如何工作的。我相信我已经得到了所有4行代码的答案,并将在这里展示。如果有人能证实我对这4个例子的回答,我会非常高兴

因此,我下面的4个示例是为ndb而不是db编写的

story = ja4.get()         #line 1 (Story(key=Key('Story', 5294973121462272), author=Key('Author', 5857923074883584), pov=u'p4'))
story.author.get().name   #line 2 (a1)
story.author.get()        #line 3 (Author(key=Key('Author',5857923074883584), name=u'a1'))
for astory in Story.query(Story.author == story.author.get().key):
     print astory.pov     #line 4 (p1, p2, p4)
以上更新

class Author(db.Model):
    name = db.StringProperty()

class Story(db.Model):
    author = db.KeyProperty(Author)  #not Reference

story = db.get(story_key)    #I can make this work, but no more.
author_name = story.author.name   #Errors are listed below.

author = db.get(author_key)
stories_by_author = author.story_set.get()
下面是我的测试数据和一些试用代码

class Author(ndb.Model):
    name = ndb.StringProperty()

class Story(ndb.Model):
    author = ndb.KeyProperty(Author)
    pov  = ndb.StringProperty()

author1 = Author(name='a1')
author2 = Author(name='a2')
author3 = Author(name='a3')
ka1 = author1.put()
ka2 = author2.put()
ka3 = author3.put()

story1 = Story(pov='p1', author=ka1)
story2 = Story(pov='p2', author=ka2)
story3 = Story(pov='p3', author=ka1)
story4 = Story(pov='p4', author=ka1)
story5 = Story(pov='p5', author=ka2)
story6 = Story(pov='p6', author=ka3)

ja1 = story1.put()
ja2 = story2.put()
ja3 = story3.put()
ja4 = story4.put()
ja5 = story5.put()
ja6 = story6.put()

以下是一个非答案:

帮自己一个忙,使用较新的
ndb
而不是
db
。在这里,语法是:

from google.appengine.ext import ndb

class Author(ndb.Model):
    name    = ndb.StringProperty()

class Story(ndb.Model):
    author  = ndb.KeyProperty(kind = Author)
    pov     = ndb.StringProperty()

以下是一个非答案:

帮自己一个忙,使用较新的
ndb
而不是
db
。在这里,语法是:

from google.appengine.ext import ndb

class Author(ndb.Model):
    name    = ndb.StringProperty()

class Story(ndb.Model):
    author  = ndb.KeyProperty(kind = Author)
    pov     = ndb.StringProperty()

使用问题中的ndb模型和数据,下面的4条“线”回答了我的问题

story = ja4.get()         #line 1 (Story(key=Key('Story', 5294973121462272), author=Key('Author', 5857923074883584), pov=u'p4'))
story.author.get().name   #line 2 (a1)
story.author.get()        #line 3 (Author(key=Key('Author',5857923074883584), name=u'a1'))
for astory in Story.query(Story.author == story.author.get().key):
     print astory.pov     #line 4 (p1, p2, p4)

使用问题中的ndb模型和数据,下面的4条“线”回答了我的问题

story = ja4.get()         #line 1 (Story(key=Key('Story', 5294973121462272), author=Key('Author', 5857923074883584), pov=u'p4'))
story.author.get().name   #line 2 (a1)
story.author.get()        #line 3 (Author(key=Key('Author',5857923074883584), name=u'a1'))
for astory in Story.query(Story.author == story.author.get().key):
     print astory.pov     #line 4 (p1, p2, p4)

你有理由在ndb上使用这个吗?我不认为我反对使用ndb,但我继续使用db,所以我可以复制和粘贴旧代码。我的GAE应用程序都是免费的,我不知道ndb是否免费。但最重要的是,当我将代码更改为ndb时,GAE交互控制台似乎失败了。是不是我做错了什么,控制台应该可以工作?以下是一个典型错误<代码>文件“”,第10行,文件“”,第11行,故事中的AttributeError:“module”对象没有属性“ReferenceProperty”@zerowords,ndb与db不同,因此您需要更新代码,但您应该切换到ndb。我想说的是,几乎所有使用Python GAE的有经验的人都使用ndb而不是db。自从提到这个问题以来,我已经研究了一点差异,并惊讶地看到,正如我的示例所示,我正在使用ndb puts。但ndb得不到。我还惊讶地看到,ndb中的ReferenceProperty()已更改为KeyProperty()。所以我修改了我的例子,可以得到一些有用的结果。例如:
print ja4
->
Key('Story',5689697795833856)
print ja4.get()
->
Story(Key=Key('Story',5689697795833856),author=Key('author',6428569609699328),pov=u'p4')
。但我没有ndb参考说明如何使用这些结果。请查看文档中的密钥(这是KeyProperty存储的内容)。给定某个键,您可以显式检索该键引用的实体。get()这实际上比db中的引用属性要好。我总是发现我必须跳转,以防止密钥属性的解除保护,如果实体不存在,这将导致额外的错误。你有理由在ndb上使用它吗?我不认为我反对使用ndb,但我继续使用db,因此我可以复制和粘贴旧代码。我的GAE应用程序都是免费的,我不知道ndb是否免费。但最重要的是,当我将代码更改为ndb时,GAE交互控制台似乎失败了。是不是我做错了什么,控制台应该可以工作?以下是一个典型错误<代码>文件“”,第10行,文件“”,第11行,故事中的AttributeError:“module”对象没有属性“ReferenceProperty”@zerowords,ndb与db不同,因此您需要更新代码,但您应该切换到ndb。我想说的是,几乎所有使用Python GAE的有经验的人都使用ndb而不是db。自从提到这个问题以来,我已经研究了一点差异,并惊讶地看到,正如我的示例所示,我正在使用ndb puts。但ndb得不到。我还惊讶地看到,ndb中的ReferenceProperty()已更改为KeyProperty()。所以我修改了我的例子,可以得到一些有用的结果。例如:
print ja4
->
Key('Story',5689697795833856)
print ja4.get()
->
Story(Key=Key('Story',5689697795833856),author=Key('author',6428569609699328),pov=u'p4')
。但我没有ndb参考说明如何使用这些结果。请查看文档中的密钥(这是KeyProperty存储的内容)。给定某个键,您可以显式检索该键引用的实体。get()这实际上比db中的引用属性要好。我总是发现我必须跳转,以防止密钥属性的解除保护,如果实体不存在,这将导致额外的错误。