开发者

(How) can you add methods to a collection of entities in Google App Engine python?

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 i开发者_开发知识库t.

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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜