在GAE与祖先合作

编程入门 行业动态 更新时间:2024-10-17 19:29:15
本文介绍了在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与祖先合作

本文发布于:2023-11-12 20:05:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:祖先   GAE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!