[英]Adding a StructuredProperty to a Model in NDB
(couldn't think of a better title :S ) (想不出更好的头衔:S)
So I've recently changed from db to ndb and i can't get one part to work. 所以我最近从db更改为ndb,我无法让一部分工作。 I have this tutorial model that has chapters, so I am using 'ndb.StructuredProperty' to associate the model Chapter to the tutorial. 我有这个包含章节的教程模型,因此我使用'ndb.StructuredProperty'将模型章节与教程相关联。 I can create the tutorials and the chapter with no problems but i can't point the chapters to the tutorial. 我可以创建教程和章节没有问题,但我不能指出章节到教程。
The Tutorial Model: 教程模型:
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)
The Edit Class: 编辑类:
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')
Using this code the tutorial is created but i get this error: 使用此代码创建教程但我收到此错误:
tutorialInstance.chapters = chap
AttributeError: 'NoneType' object has no attribute 'chapters'
You seem to be confused. 你好像很困惑。 When using StructuredProperty
, the contained object doesn't have its own ID or key -- it's just more properties with funny names in the outer object. 使用StructuredProperty
,包含的对象没有自己的ID或键 - 它只是外部对象中有趣名称的更多属性。 Perhaps you want a repeated KeyProperty
linking the book to its chapters rather than having all the chapters contained inside the book? 也许你想要一本重复的KeyProperty
本书与其章节KeyProperty
联 ,而不是将所有章节都包含在书中? You have to choose one or the other. 你必须选择其中一个。
Update: with the help of @nizz, changing 更新:在@nizz的帮助下,改变
tutorialInstance = tutorial.get()
tutorialInstance.chapters = chap
to: 至:
tutorialInstance = ndb.Key('Tutorial', int(tutID)).get()
tutorialInstance.chapters.append(chap)
worked perfectly. 工作得很好。
您正在处理列表...您需要将对象附加到列表中
tutorialInstance.chapters.append(chap)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.