【问题标题】:(How) can you add methods to a collection of entities in Google App Engine python?(如何)您可以向 Google App Engine python 中的实体集合添加方法吗?
【发布时间】:2010-08-17 18:35:38
【问题描述】:

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

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

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

【问题讨论】:

  • 你的问题很模糊。请解释“实体集合”是什么意思。你是说上课吗?这是 RPC 吗?混音?

标签: python google-app-engine collections model


【解决方案1】:

您正在寻找的是一对多的关系。没有 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)

【讨论】:

    猜你喜欢
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    相关资源
    最近更新 更多