O'Reilly logo

Using Google App Engine by Charles Severance

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

References Between Data Models

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.

Adding a chat feature

Figure 8-8. Adding a chat feature

The data model for ChatMessage 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 linking each ChatMessage object to the corresponding User object.

The following model shows how we represent a Reference in the ChatMessage model:

class ChatMessage(db.Model):
  user = db.ReferenceProperty()
  text = db.StringProperty()
  created = db.DateTimeProperty(auto_now=True)

The ChatMessage model includes a user property, which is indicated as a reference property. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required