Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is just an extension to <a href="https://stackoverflow.com/questions/2687724/copy-an-entity-in-google-app-engine-datastore-in-python-without-knowing-property/2712401#2712401">Nick Johnson's excellent code</a> to address the problems highlighted by Amir in the comments:</p> <ol> <li>The db.Key value of the ReferenceProperty is no longer retrieved via an unnecessary roundtrip to the datastore.</li> <li>You can now specify whether you want to skip DateTime properties with the <code>auto_now</code> and/or <code>auto_now_add</code> flag.</li> </ol> <p>Here's the updated code:</p> <pre><code>def clone_entity(e, skip_auto_now=False, skip_auto_now_add=False, **extra_args): """Clones an entity, adding or overriding constructor attributes. The cloned entity will have exactly the same property values as the original entity, except where overridden. By default it will have no parent entity or key name, unless supplied. Args: e: The entity to clone skip_auto_now: If True then all DateTimeProperty propertes will be skipped which have the 'auto_now' flag set to True skip_auto_now_add: If True then all DateTimeProperty propertes will be skipped which have the 'auto_now_add' flag set to True extra_args: Keyword arguments to override from the cloned entity and pass to the constructor. Returns: A cloned, possibly modified, copy of entity e. """ klass = e.__class__ props = {} for k, v in klass.properties().iteritems(): if not (type(v) == db.DateTimeProperty and ((skip_auto_now and getattr(v, 'auto_now')) or (skip_auto_now_add and getattr(v, 'auto_now_add')))): if type(v) == db.ReferenceProperty: value = getattr(klass, k).get_value_for_datastore(e) else: value = v.__get__(e, klass) props[k] = value props.update(extra_args) return klass(**props) </code></pre> <p>The first <code>if</code> expression is not very elegant so I appreciate if you can share a better way to write it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload