向NDB中的模型添加StructuredProperty [英] Adding a StructuredProperty to a Model in NDB

查看:133
本文介绍了向NDB中的模型添加StructuredProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(无法想象一个更好的标题: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(重复= True)
votes = ndb.IntegerProperty()
created = ndb.DateTimeProperty(auto_now_add = True)
last_modified = ndb.DateTimeProperty(auto_now = True)
章节= ndb。 StructuredProperty(Chapter,repeated = True)

编辑类:

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

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

如果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'对象没有属性'章节'


解决方案

更新:借助@nizz,
change

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


到:

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

完美运作。

(couldn't think of a better title :S )

So I've recently changed from db to ndb and i can't get one part to work. I have this tutorial model that has chapters, so I am using 'ndb.StructuredProperty' to associate the model Chapter to the tutorial. 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'

解决方案

Update: with the help of @nizz, changing

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

to:

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

worked perfectly.

这篇关于向NDB中的模型添加StructuredProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆