gpt4 book ai didi

python - 如何在不显式调出每个属性的情况下将 GAE 中一种类型的所有实体复制到另一种类型

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:54 24 4
gpt4 key购买 nike

我们如何使用 function clone_entity(),如 Copy an entity in Google App Engine datastore in Python without knowing property names at 'compile' time 中所述将值复制到不同种类的实体? (因为 key 也被复制,所以克隆发生在相同的 Kind 中,所以上面链接中的解决方案不适用于这个特定目的!)

尝试了以下(和其他变体但无济于事)

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()

(需要将所有实体从OrigKind复制到NewKind)

最佳答案

您需要 clone_entity 的修改版本:

original clone method 有一些陷阱在原始实现的答案中进行了讨论。

def clone_entity(e, to_klass, **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
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 = dict((k, v.__get__(e, klass)) for k, v in klass.properties().iteritems())
props.update(extra_args)
return to_klass(**props)

# Use the clone method
query = db.GqlQuery("SELECT * FROM OrigKind")
results = query.fetch(10);

for user in results:
new_entry = clone_entity(user, NewKind)
new_entry.put()

关于python - 如何在不显式调出每个属性的情况下将 GAE 中一种类型的所有实体复制到另一种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14314344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com