在 GAE 中与祖先一起工作 [英] Working with ancestors in GAE

查看:20
本文介绍了在 GAE 中与祖先一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只希望有人向我证实我正在以正确的方式做事.

I only want that someone confirm me that I'm doing things in the right way.

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

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.

我的疑问是:我是否需要所有的连锁书章节来搜索页面?

My doubt is: do I need all the chain book-chapter to search a page?

例如(我在 NDB):

For example (I'm in NDB):

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

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

class Page(ndb.Model):
    # Search by id
    @classmethod
    def by_id(cls, id, chapter):
        return Page.get_by_id(long(id), parent=chapter.key)

实际上,当我需要搜索页面以显示其内容时,我会像这样在 url 中传递完整的链:

Actually, when I need to search a Page to display its contents, I'm passing the complete chain in the url like this:

getPage?bookId=5901353784180736&chapterId=5655612935372800&pageId=1132165198169

getPage?bookId=5901353784180736&chapterId=5655612935372800&pageId=1132165198169

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

So, in the controller, I make this:

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

    if bookId and chapterId and pageId:
        # Must be a digit
        if bookId.isdigit() and chapterId.isdigit() and pageId.isdigit():
            # Get the book
            book = Book.by_id(bookId)

            if book:
                # Get the chapter
                chapter = Chapter.by_id(chapterId, book)

                if chapter:
                    # Get the page
                    page = Page.by_id(pageId, chapter)

这是正确的方法吗?我必须在 URL 中始终拥有完整的链才能获得链的最后一个元素吗?

Is this the right way? Must I have always the complete chain in the URL to get the final element of the chain?

如果这是正确的,我想这种使用 NDB 的工作方式不会对数据存储产生任何影响,因为对该页面的重复调用总是会命中同一本书、章节和页面的 NDB 缓存(因为我'通过 id 获取,不是 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 book, chapter and page (because I'm getting by id, is not a fetch command). Is my suppose correct?

推荐答案

不,没有必要这样做.关键是键是路径:您可以动态构建它们,并且只有在您拥有完整的数据存储时才访问数据存储.在你的情况下,它是这样的:

No, there's no need to do that. The point is that keys are paths: you can build them up dynamically and only hit the datastore when you have a complete one. In your case, it's something like this:

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

有关更多示例,请参阅NDB 文档.

See the NDB docs for more examples.

这篇关于在 GAE 中与祖先一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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