动态向Google Datastore实体添加属性 [英] Add properties to Google Datastore entity dynamically

查看:64
本文介绍了动态向Google Datastore实体添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下用例:

我有一个接受字符串列表的方法.对于每个字符串,我需要在现有的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

示例:我有一个具有属性fnamelname的现有实体Person. 如果输入列表包含字符串-addresscity,则需要更新实体Person以包括这些新属性addresscity.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆