【问题标题】:Adding a StructuredProperty to a Model in NDB向 NDB 中的模型添加 StructuredProperty
【发布时间】:2013-01-30 19:15:18
【问题描述】:

(想不出更好的标题:S)

所以我最近从 db 更改为 ndb,但我无法让其中一部分工作。我有这个有章节的教程模型,所以我使用“ndb.StructuredProperty”将模型章节与教程相关联。 我可以毫无问题地创建教程和章节,但我无法将章节指向教程。

教程模型:

class Tutorial(ndb.Model):
    title = ndb.StringProperty(required=True)
    presentation = ndb.TextProperty(required=True)
    extra1 = ndb.TextProperty()
    extra2 = ndb.TextProperty()
    extra3 = ndb.TextProperty()
    tags = ndb.StringProperty(repeated=True)
    votes = ndb.IntegerProperty()
    created = ndb.DateTimeProperty(auto_now_add=True)
    last_modified = ndb.DateTimeProperty(auto_now=True)
    chapters = ndb.StructuredProperty(Chapter, repeated=True)

编辑类:

class EditTut(FuHandler):
    def get(self):
        ...
        ...

    def post(self):
        editMode = self.request.get('edit')

        if editMode == '2':
            ...
            ...

        elif editMode == '1':
            tutID = self.request.cookies.get('tut_id', '')
            tutorial = ndb.Key('Tutorial', tutID)
            title = self.request.get("chapTitle")
            content = self.request.get("content")
            note = self.request.get("note")

            chap = Chapter(title=title, content=content, note=note)
            chap.put()
            tutorialInstance = tutorial.get()
            tutorialInstance.chapters = chap
            tutorialInstance.put()

            self.redirect('/editTut?edit=%s' % '0')
        else:
            self.redirect('/editTut?edit=%s' % '1')

使用此代码创建教程,但我收到此错误:

tutorialInstance.chapters = chap
AttributeError: 'NoneType' object has no attribute 'chapters'

【问题讨论】:

  • 你不应该在你的章节中调用 'put()' - StructuredProperties 被存储为其包含实体的一部分,并且单独保存它们也没有什么意义。
  • 你是对的,但现在我遇到了问题。我想获取章节的 ID(将其传递给 URL),但没有

标签: python google-app-engine app-engine-ndb


【解决方案1】:

你似乎很困惑。当使用StructuredProperty 时,包含的对象没有自己的 ID 或键——它只是在外部对象中有更多具有有趣名称的属性。也许您想要重复的KeyProperty链接本书到它的章节,而不是让所有章节都包含在本书中内部?您必须选择其中之一。

【讨论】:

  • 我只是想要一种方法来选择一个特定的章节......我现在没有 ID 或密钥,这是我的问题,我想知道识别它的最佳方法。但是......从我一直在阅读的内容和我一直遇到的问题来看......我的设计可能都是错误的。我将创建一个新帖子,解释我想做什么以及我想做什么。
【解决方案2】:

您正在处理一个列表...您需要将对象附加到列表中

tutorialInstance.chapters.append(chap)

【讨论】:

  • 谢谢,但我仍然必须在我的答案中做出改变
  • 根据developers.google.com/appengine/docs/python/ndb/… get_by_id(id, parent=None, app=None, namespace=None, **ctx_options) 按ID返回一个实体。这实际上只是 Key(cls, id).get() 的简写,因此,不同之处在于将 tutID 转换为整数,而不是更改使用的函数
  • class.. 在你的情况下教程:Tutorial.get_by_id(tutId) 等于 ndb.Key('Tutorial', tutId).get()
  • 那样不行,我得到这个错误:AttributeError: 'NoneType' object has no attribute 'chapters'
  • 正如我上面解释的,不同之处在于 int(...) 部分......可能当您创建教程时,您传递的 id 类型是整数,而不是字符串,所以您需要将整数传递给 id。所以,Tutorial.get_by_id(int(tutId)) 等于 ndb.Key('Tutorial', int(tutId)).get() (和上面的差不多)
【解决方案3】:

更新:在@nizz 的帮助下, 改变

tutorialInstance = tutorial.get()
tutorialInstance.chapters = chap

到:

tutorialInstance = ndb.Key('Tutorial', int(tutID)).get()
tutorialInstance.chapters.append(chap)

完美运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多