Skip to content

Instantly share code, notes, and snippets.

@jakalada
Created March 15, 2010 17:47
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/333094 to your computer and use it in GitHub Desktop.
Save jakalada/333094 to your computer and use it in GitHub Desktop.
quote from GAE document - Entity
# ref: http://code.google.com/intl/ja/appengine/docs/python/datastore/entitiesandmodels.html#Properties_and_Types
# reserved attribute names by the Model class
#
# all, app, copy, delete, entity, entity_type
# fields, from_entity, get, gql, instance_properties
# is_saved, key, key_name, kind, parent, parent_key
# properties, put, setdefault, to_xml, update
# --------------
# StringProperty
# ref: http://code.google.com/intl/ja/appengine/docs/python/datastore/typesandpropertyclasses.html#StringProperty
# --------------
class MyModel(db.Model):
string = db.StringProperty()
obj = MyModel()
# Python Unicode literal syntax fully describes characters in a text string.
obj.string = u"kittens"
# unicode() converts a byte string to a Unicode value using the named codec.
obj.string = unicode("kittens", "latin-1")
# A byte string is assumed to be text encoded as ASCII (the 'ascii' codec).
obj.string = "kittens"
# Short string properties can be used in query filters.
results = db.GqlQuery("SELECT * FROM MyModel WHERE string = :1", u"kittens")
# --------------
# TextProperty
# ref: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#TextProperty
# --------------
class MyModel(db.Model):
text = db.TextProperty()
obj = MyModel()
# Text() can take a Unicode value.
obj.text = db.Text(u"lots of kittens")
# Text() can take a byte string and the name of an encoding.
obj.text = db.Text("lots of kittens", "latin-1")
# If no encoding is specified, a byte string is assumed to be ASCII text.
obj.text = db.Text("lots of kittens")
# Text properties can store large values.
obj.text = db.Text(open("a_tale_of_two_cities.txt").read(), "utf-8")
# --------------
# ByteStringProperty
# ref: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#ByteStringProperty
# --------------
# --------------
# BlobProperty
# ref: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#BlobProperty
# --------------
class MyModel(db.Model):
blob = db.BlobProperty()
obj = MyModel()
obj.blob = db.Blob(open("image.png").read())
# --------------
# ListProperty
# ref: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#ListProperty
# ListProperty(basestring) equal StringListProperty
# ref: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#StringListProperty
# --------------
class MyModel(db.Model):
numbers = db.ListProperty(long)
obj = MyModel()
obj.numbers = [2, 4, 6, 8, 10]
obj.numbers = ["hello"] # ERROR: MyModel.numbers must be a list of longs.
# Get all entities where numbers contains a 6.
results = db.GqlQuery("SELECT * FROM MyModel WHERE numbers = 6")
# Get all entities where numbers contains at least one element less than 10.
results = db.GqlQuery("SELECT * FROM MyModel WHERE numbers < 10")
# --------------
# ReferenceProperty
# ref: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#ReferenceProperty
# --------------
class FirstModel(db.Model):
prop = db.IntegerProperty()
class SecondModel(db.Model):
reference = db.ReferenceProperty(FirstModel)
obj1 = FirstModel()
obj1.prop = 42
obj1.put()
obj2 = SecondModel()
# A reference value is the key of another entity.
obj2.reference = obj1.key()
# Assigning a model instance to a property uses the entity's key as the value.
obj2.reference = obj1
obj2.put()
obj2.reference.prop = 999
obj2.reference.put()
results = db.GqlQuery("SELECT * FROM SecondModel")
another_obj = results.fetch(1)[0]
v = another_obj.reference.prop
obj1 = obj2.reference
if not obj1:
# Referenced entity was deleted.
# To fetch and iterate over every SecondModel entity that refers to the
# FirstModel instance obj1:
for obj in obj1.secondmodel_set:
# ...
class FirstModel(db.Model):
prop = db.IntegerProperty()
# This class raises a DuplicatePropertyError with the message
# "Class Firstmodel already has property secondmodel_set"
class SecondModel(db.Model):
reference_one = db.ReferenceProperty(FirstModel)
reference_two = db.ReferenceProperty(FirstModel)
#
class FirstModel(db.Model):
prop = db.IntegerProperty()
# This class runs fine
class SecondModel(db.Model):
reference_one = db.ReferenceProperty(FirstModel,
collection_name="secondmodel_reference_one_set")
reference_two = db.ReferenceProperty(FirstModel,
collection_name="secondmodel_reference_two_set")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment