在GAE与祖先合作 [英] Working with ancestors in GAE

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

问题描述



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



我很清楚,为了按ID搜索章节,我需要通过祖先查询来搜索书籍。



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



例如(I在NDB中):

  class Book(ndb.Model):
#按ID搜索
@classmethod
def by_id(cls,id):
返回Book.get_by_id(long(id))

class Chapter(ndb.Model):
#Search通过id
@classmethod
def by_id(cls,id,book):
返回Chapter.get_by_id(long(id),parent = book.key)

class Page(ndb.Model):
#按ID搜索
@classmethod
def by_id(cls,id,chapter):
返回Page.get_by_id(long(id), parent = chapter.key)

实际上,当我需要搜索一个页面来显示它的内容时,我会像这样在URL中传递整个链:



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

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

  def get(self):
#获取id参数
bookId = self.request.get('bookId')
chapterId ='self.request.get'('chapterId')
pageId = self.request.get('pageId')
$ b $如果bookId和chapterId和pageId:
#如果是bookId.isdigit()和chapterId.isdigit()和pageId.isdigit():
#,则必须是数字
获取图书
book = Book.by_id(bookId)

如果book:
#获取章节
chapter = Chapter.by_id(chapterId,book)

如果章节:
#获取页面
p age = Page.by_id(pageId,chapter)

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



如果这是正确的,我认为这种使用NDB的工作方式,对数据存储没有任何影响,因为对这个页面的重复调用总是为同一本书,章节和页面命中NDB缓存(因为我是通过id获取的,而不是获取命令)。我的假设是正确的吗?

解决方案

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

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

请参阅 NDB文档以获取更多示例。


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)

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?

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)

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

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)

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

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()

See the NDB docs for more examples.

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

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