Here is another way to check if an instance is saved without
checking _from_entity.
from google.appengine.ext import db
import types
class A(db.Model): # works with Expando too
name = db.StringProperty(required=True)
def __init__(self, _is_new=True, key_name=None, _app=None,
_from_entity=False, parent=None, **kwds):
if not parent and type(_is_new) is not types.BooleanType:
parent = _is_new
print "# before __init__: is new %s" % bool(_is_new)
super(db.class_for_kind(self.kind()), self).__init__(parent,
key_name, _app, _from_entity, **kwds)
print "# after __init__: is new %s" % bool(_is_new)
a = A(key_name="ka",name="a")
# before __init__: is new True
# after __init__: is new True
a.put()
b = A(parent=a,key_name="kb", name="b")
# before __init__: is new True
# after __init__: is new True
c = A.all()[0]
# before __init__: is new False
# after __init__: is new False
c = A.get(a.key())
# before __init__: is new False
# after __init__: is new False
c = A.get_by_key_name(a.key().name())
# before __init__: is new False
# after __init__: is new False
When you try to get model instances using db.Model.get,
Model.get_by_key_name, etc. db.get(keys) will be called to get the
entities from the datastore and convert them to models instances using
the class method Model.from_entity(entity). Query fetches also use
from_entity to return model instances.
In from_entity, the model instance is called this way: cls(None,
_from_entity=True, **entity_values). It passes None to the parent
argument; when you retrieve an entity
parent and key_name are never passed to the model's __init__ so it is
safe
to grab it with _is_new and make the default True.
Now that I wasted more time with this ad-hockery, I am going to stick
with the _from_entity method which is much cleaner.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---