动态向Google Datastore实体添加属性 [英] Add properties to Google Datastore entity dynamically
问题描述
我有一个以下用例:
我有一个接受字符串列表的方法.对于每个字符串,我需要在现有的Google数据存储实体A
I have a method that accepts a list of strings. For each of the strings, I need to create a property under an existing google data store entity A
示例:我有一个具有属性fname
和lname
的现有实体Person
.
如果输入列表包含字符串-address
,city
,则需要更新实体Person
以包括这些新属性address
和city
.
Example: I have an existing entity Person
with properties fname
and lname
.
If the input list has strings - address
, city
, I need to update the entity Person
to include these new properties address
and city
.
我正在用Python编写此用例.关于如何实现此目标的任何建议?
I'm coding this use case in Python. Any suggestions on how I can achieve this?
推荐答案
So the best way to do this is to let your class inherit ndb.Expando
. The difference between Expando
and Model
is that you can always add attributes to an Expando
entity and be able to store it in the Datastore.
知道了这一点,有几种方法可以进行,但是我想您还需要使用Python的setattr(object, name, value)
方法从字符串中传递属性名称.
Knowing this, there are several ways to proceed, but I am guessing you’re also going to need to use Python’s setattr(object, name, value)
method to pass the attribute name from a string.
这篇关于动态向Google Datastore实体添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!