(想不出更好的标题: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'