How do we use function clone_entity()
as described in Copy an entity in Google App Engine datastore in Python without knowing property names at 'compile' time to copy the values to an entity in a different Kind? (since the keys get copied as well so cloning happens in the same Kind so the solution at the above link does not work for this particular purpose!)
Tried the following (and other variations but to no avail)
query = db.GqlQuery("SELECT * FROM OrigKind")
results = query.fetch(10);
for user in results:
new_entry = models.NewKind()
new_entry_complete_key = new_entry.put()
new_entry = clone_entity(user, Key=new_entry_complete_key)
new_entry.put()
(need to copy all entities from OrigKind to NewKind)
You need a modified version of clone_entity:
There are some pitfalls to the original clone method that are discussed in the answers of the original implementation.
It just wrote a utility to copy enties from one appid to another and to zip entities of a kind. This utility makes an exact clone, including keys, NDB repeated properties, serving_urls and blobs referenced in the kind. To make this work I have to know the property types of the entities. I use Python 27 and NDB, but the utility also transfers db.Models.
Here is the code to find all the property types for a kind :
In the above code I wrap a pager (paged transfer using a cursor) for db or NDB to transfer the entities between the GAE appid's.
Based on the properties I can encode and decode the properties to transfer the model. To do this I first create a dict of the entities using
NDB : entity.to_dict() or db: entity.to_dict()
. And I add the key to the dict. Now I can encode the properties of the entity and pickle the result to transfer the encoded entity:I would like to add a couple of things to Shay's answer:
.