You can model relationships between entities by storing entity keys as property values. The Python data modeling interface includes several powerful features for managing relationships.
db.ReferenceProperty declaration describes a
relationship between one model class and another. It stores the key of an
entity as the property value. The first argument to the
db.ReferenceProperty constructor is the model class of the
kind of entity referenced by the property. If someone creates a
relationship to an entity that is not of the appropriate kind, the
assignment raises a
You can assign a data object directly to the property. The property
declaration stores the key of the object as the property’s value to create
the relationship. You can also assign a
class Book(db.Model): title = db.StringProperty() author = db.StringProperty() class BookReview(db.Model): book = db.ReferenceProperty(Book, collection_name='reviews') b = Book() b.put() br = BookReview() br.book = b # sets br's 'book' property to b's key br.book = b.key() # same thing
We’ll explain what
collection_name does in a
The referenced object must have a “complete” key before it can be assigned to a reference property. A key is complete when it has all of its parts, including the string name or the system-assigned numeric ID. If you create a new object without a key name, the key is not complete until you save the object. When you save the object, the system completes ...