Hi Takashi, If the entity being put has neither key name nor id, a new id will be created for it when it's put, so it will always result in a new entity being inserted rather than an existing entity being replaced. Likewise, if you see an entity with an id being put, it is almost certainly an update to an existing entity (unless someone was messing around with the low-level API), or the entity was deleted and is now being reinserted.
If the entity being put has a key name, the only way to tell for certain if it's replacing an existing entity is to do the put operation in a transaction, and do a get on the key first. -Nick Johnson On Thu, Jul 30, 2009 at 7:19 AM, Takashi Matsuo <[email protected]>wrote: > > Hi, > > I'm writing a capability for registering db_hook dynamically. The code > snippet follows the mail body. > It works well except my code can not distinguish newly created entity > from updated entity. > > I'd like to know how can I know if the entity is created or just > updated. What is the most appropriate way? > > Regards, > > > ------------------------------------------------------------------------------------------ > from google.appengine.api import apiproxy_stub_map > from google.appengine.api import datastore > from google.appengine.ext import db > > post_save_hooks = {} > > def register_post_save_hook(func, model): > global post_save_hooks > kind = model.kind() > func_list = post_save_hooks.get(kind, None) > if func_list is None: > func_list = [] > func_list.append(func) > post_save_hooks[kind] = func_list > > > def execute_hooks(kind, key, entity): > func_list = post_save_hooks.get(kind, None) > if func_list is not None: > entity.key_.CopyFrom(key) > e = datastore.Entity._FromPb(entity) > instance = db.class_for_kind(kind).from_entity(e) > for func in func_list: > func(instance) > > def db_hook(service, call, request, response): > if call == 'Put': > from kay.utils.db_hook import execute_hooks > for key, entity in zip(response.key_list(), request.entity_list()): > kind = model_name_from_key(key) > execute_hooks(kind, key, entity) > > apiproxy_stub_map.apiproxy.GetPostCallHooks().Append( > 'db_hook', db_hook, 'datastore_v3') > > ------------------------------------------------------------------------------------------ > > -- > Takashi Matsuo > > > > -- Nick Johnson, Developer Programs Engineer, App Engine --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
