在NDB中使用密钥来检索实体 [英] Using Key in NDB to retrieve an entity

查看:86
本文介绍了在NDB中使用密钥来检索实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的结构:书有章节(祖先=书)有页(祖先=章)



我很清楚,搜索按ID分章节,我需要书籍通过祖先查询进行搜索。我今天已经知道,如果我拥有所有的关键字,我可以直接检索实体,而不需要先取得书本,然后是章节,然后是页面:

  page_key = ndb.Key('Book',long(bookId),'Chapter',long(chapterId),'Page',long(pageId))
page = page_key.get()

我的疑问是:例如,页码,我必须先拿到这一章?



例如:

  class Book(ndb.Model):
name = ndb.StringProperty(required = True)

#按ID搜索
@classmethod
def by_id(cls ,id):
return Book.get_by_id(long(id))

class Chapter(ndb.Model):
name = ndb.StringProperty(required = True)

#按ID搜索
@classmethod
def by_id(cls,id,book):
return Chapter.get_by_id(long(id),parent = book.key)

class Page(ndb.Model):
pageNumb er = ndb.IntegerProperty(default = 1)
content = ndb.StringProperty(required = True)

#按页搜索
@classmethod
def by_page(cls ,数字,章节):
page = Page.query(ancestor = chapter.key)
page = page.filter(Page.pageNumber == int(数字))
返回page.get ()

实际上,当我需要搜索页面来显示其内容时,我正在执行此操作:

getPage?bookId = 5901353784180736&chapterId = 5655612935372800& page = 2

所以,在控制器中,我做这个:

  def get(self):
#获取id参数
bookId = self .request.get('bookId')
chapterId = self.request.get('chapterId')
pageNumber = self.request.get('page')

if bookId和chapterId和pageNumber:
#如果是bookId.isdigit()和chapterId.isdigit()和pageNumber.isdigit():$ b,则必须是数字
$ b $获取章节
chapter_key = ndb.Key('Book',long(bookId),'Chapter',long(chapterId))
chapter = chapter_key.get()

if chapter:
#获取页面
page = Page.by_number(pageNumber,chapter)

这是正确的方式,还是有更好的方法我错过了我只能访问数据存储的地方,而不是两个?



如果这是正确的,我想这种使用NDB的工作方式对数据存储没有任何影响,因为对这个页面的重复调用总是碰到相同章节和页面的NDB缓存(因为我正在使用 get()方法,它不是一个 fetch()命令)。我的假设是正确的吗?

解决方案

您可以通过做一个祖先查询而不是get:

  chapter_key = ndb.Key('Book',long(bookId),'Chapter',long chapterId))
page = Page.query(Page.pageNumber == pageNumber,ancestor = chapter_key)


I have this structure: Books that have Chapters (ancestor=Book) that have Pages (ancestor=Chapter)

It is clear for me that, to search for a Chapter by ID, I need the book to search by ancestor query. And I've learn today that if I have all the keys, I can retrieve directly the entity without need of get first the book, then the chapter and then the page, in this way:

page_key = ndb.Key('Book', long(bookId), 'Chapter', long(chapterId), 'Page', long(pageId))
page = page_key.get()

My doubt is: to get the page by, for example, page number, must I get first the chapter?

For example :

class Book(ndb.Model):
    name = ndb.StringProperty(required=True)

    # Search by id
    @classmethod
    def by_id(cls, id):
        return Book.get_by_id(long(id))

class Chapter(ndb.Model):
    name = ndb.StringProperty(required=True)

    # Search by id
    @classmethod
    def by_id(cls, id, book):
        return Chapter.get_by_id(long(id), parent=book.key)

class Page(ndb.Model):
    pageNumber = ndb.IntegerProperty(default=1)
    content = ndb.StringProperty(required=True)

    # Search by page
    @classmethod
    def by_page(cls, number, chapter):
        page = Page.query(ancestor=chapter.key)
        page = page.filter(Page.pageNumber == int(number))
        return page.get()

Actually, when I need to search the Page to display its contents, I'm doing this:

getPage?bookId=5901353784180736&chapterId=5655612935372800&page=2

So, in the controller, I make this:

def get(self):
    # Get the id parameters
    bookId = self.request.get('bookId')
    chapterId = self.request.get('chapterId')
    pageNumber = self.request.get('page')

    if bookId and chapterId and pageNumber:
        # Must be a digit
        if bookId.isdigit() and chapterId.isdigit() and pageNumber.isdigit():
            # Get the chapter
            chapter_key = ndb.Key('Book', long(bookId), 'Chapter', long(chapterId))
            chapter = chapter_key.get()

            if chapter:
                # Get the page
                page = Page.by_number(pageNumber, chapter)

Is this the right way or there is a better way I'm missing where I can do only an access to datastore, instead two?

If this is right, I suppose that this way of work, using NDB, does not have any impact on the datastore, because repeated calls to this page always hit the NDB cache for the same chapter and page (because I'm using get() method, it is not a fetch() command). Is my suppose right?

解决方案

You can do this in one go by doing an ancestor query, rather than a get:

chapter_key = ndb.Key('Book', long(bookId), 'Chapter', long(chapterId))
page = Page.query(Page.pageNumber==pageNumber, ancestor=chapter_key)

这篇关于在NDB中使用密钥来检索实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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