As our application gets more complicated, we will create multiple models. A very important part of building multiple models is how connections are made between models to define the relationships between the models. App Engine calls these connections references because we store a reference from one model instance to another model instance.
So let’s add a multiuser chat feature to our example
application. This feature will require a new model in which to store the
ChatMessage objects. We will allow
users to chat only when they are logged in, and we will record which
user issued which chat message, as shown in Figure 8-8.
Figure 8-8. Adding a chat feature
The data model for
objects has a text variable for the message, a date variable that
indicates when the message was posted, and a reference to the
User object that posted the chat message. We
could optionally store the user as a string value and store the account
name (“csev” or “sally”), but a better practice is to add a reference
ChatMessage object to
The following model shows how we represent a
Reference in the
class ChatMessage(db.Model): user = db.ReferenceProperty() text = db.StringProperty() created = db.DateTimeProperty(auto_now=True)
ChatMessage model includes
user property, which is indicated as a reference property. ...