Skip to content

Instantly share code, notes, and snippets.

@jakalada
Created March 15, 2010 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakalada/333045 to your computer and use it in GitHub Desktop.
Save jakalada/333045 to your computer and use it in GitHub Desktop.
quote from GAE document - Model
# ref: http://code.google.com/intl/ja/appengine/docs/python/datastore/entitiesandmodels.html
# -----
# Model
# ref: http://code.google.com/intl/ja/appengine/docs/python/datastore/modelclass.html
# -----
from google.appengine.ext import db
class Pet(db.Model):
name = db.StringProperty(required=True)
type = db.StringProperty(
required=True, choices=set(["cat", "dog", "bird"])
)
birthdate = db.DateProperty()
weight_in_pounds = db.IntegerProperty()
spayed_or_neutered = db.BooleanProperty()
owner = db.UserProperty(required=True)
from google.appengine.api import users
pet = Pet(name="Fluffy",
type="cat",
owner=users.get_current_user())
pet.weight_in_pounds = 24
# -------------
# Expando Model
# ref: http://code.google.com/intl/ja/appengine/docs/python/datastore/expandoclass.html
# -------------
class Person(db.Expando):
first_name = db.StringProperty()
last_name = db.StringProperty()
hobbies = db.StringListProperty()
p = Person(first_name="Albert", last_name="Johnson")
p.hobbies = ["chess", "travel"]
p.chess_elo_rating = 1350
p.travel_countries_visited = ["Spain", "Italy", "USA", "Brazil"]
p.travel_trip_count = 13
p1 = Person()
p1.favorite = 42
p1.put()
p2 = Person()
p2.favorite = "blue"
p2.put()
p3 = Person()
p3.put()
people = db.GqlQuery("SELECT * FROM Person WHERE favorite < :1", 50)
# people has p1, but not p2 or p3
people = db.GqlQuery("SELECT * FROM Person WHERE favorite > :1", 50)
# people has no results
# ------------------
# Poliymorphic Model
# ref: http://code.google.com/intl/ja/appengine/docs/python/datastore/polymodelclass.html
# ------------------
from google.appengine.ext import db
from google.appengine.ext.db import polymodel
class Contact(polymodel.PolyModel):
phone_number = db.PhoneNumberProperty()
address = db.PostalAddressProperty()
class Person(Contact):
first_name = db.StringProperty()
last_name = db.StringProperty()
mobile_number = db.PhoneNumberProperty()
class Company(Contact):
name = db.StringProperty()
fax_number = db.PhoneNumberProperty()
p = Person(phone_number='1-206-555-9234',
address='123 First Ave., Seattle, WA, 98101',
first_name='Alfred',
last_name='Smith',
mobile_number='1-206-555-0117')
p.put()
c = Company(phone_number='1-503-555-9123',
address='P.O. Box 98765, Salem, OR, 97301',
name='Data Solutions, LLC',
fax_number='1-503-555-6622')
c.put()
for contact in Contact.all():
print 'Phone: %s\nAddress: %s\n\n'
% (contact.phone, contact.address)
for person in Person.all()
print 'Phone: %s\nAddress: %s\n\n'
% (contact.phone, contact.address)
for company in Company.all()
print 'Phone: %s\nAddress: %s\n\n'
% (contact.phone, contact.address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment