(如何)可以向 Google App Engine python 中的实体集合添加方法?

发布于 2024-09-15 06:55:03 字数 449 浏览 3 评论 0原文

在 GAE-python 中,您可以通过为子模型分配引用属性来建模一对多关系。

class Book(db.Model):
   title = db.StringProperty()
class Author(db.Model):
   book = db.ReferenceProperty(Book, collection_name = 'authors')
   is_primary = db.BooleanProperty()
   name = db.StringProperty()

这样,我可以访问图书对象实例的 authors 属性,以获取引用它的所有 Author 实例的可查询列表。

我想要做的是用一个方法补充 authors 属性。基于前一个例子的一个简单示例是一个仅返回按字母顺序排序的主要作者的函数。这样的语法是什么?

In GAE-python, you can model a one-to-many relationship by assigning a reference property to the child model.

class Book(db.Model):
   title = db.StringProperty()
class Author(db.Model):
   book = db.ReferenceProperty(Book, collection_name = 'authors')
   is_primary = db.BooleanProperty()
   name = db.StringProperty()

This way, I can access the authors property of a book object instance to get a query-able list of all the Author instances that reference it.

What I want to do is supplement the authors property with a method. A trivial example based on the preceding one would be a function that returns just the primary authors sorted in alphabetical. What's the syntax for something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉 2024-09-22 06:55:13

您正在寻找的是一对多的关系。没有 db.Referencelist 属性。我想这会对您有所帮助。

class Book(db.Model):
   title = db.StringProperty()
class Author(db.Model):
   is_primary = db.BooleanProperty()
   name = db.StringProperty()

book = Book(title="what is life")
book.put()

author1 = Author(parent=book,is_primary=true,name='author1')
author1.put()

author2 = Author(parent=book,is_primary=true,name='author2')
author2.put()

# from book to authors
book_authors = Author.all().ancestor(book)

What you are looking for is one to many realtionship. There is no db.Referencelist property.I guess this will help you out.

class Book(db.Model):
   title = db.StringProperty()
class Author(db.Model):
   is_primary = db.BooleanProperty()
   name = db.StringProperty()

book = Book(title="what is life")
book.put()

author1 = Author(parent=book,is_primary=true,name='author1')
author1.put()

author2 = Author(parent=book,is_primary=true,name='author2')
author2.put()

# from book to authors
book_authors = Author.all().ancestor(book)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文