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

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

问题描述

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

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

我很清楚,要按 ID 搜索章节,我需要按祖先查询搜索书.我今天了解到,如果我拥有所有密钥,我可以直接检索实体,而无需先获取书,然后是章节,然后是页面,方式如下:

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?

例如:

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

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?

如果这是正确的,我想这种使用 NDB 的工作方式不会对数据存储产生任何影响,因为对该页面的重复调用总是会命中同一章节和页面的 NDB 缓存(因为我是使用 get() 方法,它不是 fetch() 命令).我的想法对吗?

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 中使用 Key 来检索实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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