equivalent for is_saved() with ndb

244 views
Skip to first unread message

Andreas

unread,
Mar 27, 2012, 9:41:25 AM3/27/12
to google-a...@googlegroups.com
is there an equivalent of the db.Model function is_saved() with ndb.Model?

Guido van Rossum

unread,
Mar 27, 2012, 3:38:17 PM3/27/12
to google-a...@googlegroups.com
On Tuesday, March 27, 2012 6:41:25 AM UTC-7, aschmid wrote:
is there an equivalent of the db.Model function is_saved() with ndb.Model?

No; what are you trying to do? You might be able to check whether the entity has a key and if so, whether that key isn't incomplete:

if ent.key and ent.key.id():
  # It has a complete key.
else:
  # Hasn't been written, ever. 

However this can be fooled if you explicitly set the key or the id when you create an entity, e.g.

ent = Employee(id='joe')

or

ent = Employee(key=ndb.Key(Employee, 'joe'))

(These two are equivalent.)

--Guido van Rossum

Andreas

unread,
Mar 27, 2012, 4:49:54 PM3/27/12
to google-a...@googlegroups.com
exactly what im doing. i create the keys myself.

rootkey = ndb.Key('root', 'key')
a = Asset(key=ndb.Key('Asset', 'keyname', parent=rootkey))

a._has_complete_key() # returns true

in my app i work in with entity batches and sometimes it happens that i need to create ancestors for that entity which could already exist so till now i used is_saved() to filter out the entities that are already saved to the datastore to avoid putting them again (or at least trying to put them again)


--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/MeORtJLdaqEJ.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Guido van Rossum

unread,
Mar 27, 2012, 5:23:04 PM3/27/12
to google-a...@googlegroups.com
You might be able to use get_or_insert() and benefit from the in-memory cache.

--
--Guido van Rossum (python.org/~guido)

Andreas

unread,
Mar 27, 2012, 5:47:09 PM3/27/12
to google-a...@googlegroups.com
not a good option in my case.

Guido van Rossum

unread,
Mar 27, 2012, 6:19:32 PM3/27/12
to google-a...@googlegroups.com
Sorry to hear that. I guess you could manually set a flag on the
parent entity that indicates that you haven't written it yet, and
clear it in a pre_put_hook.

BTW is there any information in the parent entities? Did you know you
can create child entities for which no parent actually exists? All you
need is the parent *key*, it doesn't need to have an entity.

--Guido

Waleed Abdulla

unread,
Mar 27, 2012, 9:05:49 PM3/27/12
to google-a...@googlegroups.com
I've been looking for a good way to do something similar as well. I load (or create) an entity, do some operations on it (that may or may not change the entity), and then at the end I need to save it or skip saving it if nothing has changed. It would be nice to have something like is_dirty() which is True if the entity has never been saved or has changed since it was loaded.

Guido van Rossum

unread,
Mar 27, 2012, 9:59:48 PM3/27/12
to google-a...@googlegroups.com
But that's a completely different use case -- and old db's is_saved()
would not cover it. You can probably implement it by overriding
__setattr__ on your model class (if you promise yourself not to modify
repeated properties in-place).

--Guido

Reply all
Reply to author
Forward
0 new messages