我有以下结构:
class UserOther(ndb.Model):
other_type = ndb.StringProperty(indexed = True)
other_data = ndb.StringProperty(indexed = False)
class User(ndb.Model):
name = ndb.StringProperty(default = "NULL", indexed = False)
email = ndb.StringProperty(default = "NULL", indexed = False)
active = ndb.BooleanProperty(default = True)
others = ndb.StructuredProperty(UserOther, repeated = True)
updated_at = ndb.DateTimeProperty(auto_now = True)
如何使用用户密钥ID和other_type的字符串(例如" job")来获取并能够编辑该信息。我尝试使用祖先参数,但也许我没有正确地做到这一点。
user_key = ndb.Key("User", user_id)
user = user_key.get()
other = UserOther.query(UserOther.other_type == "job", ancestor = user_key).get()
所以如果我打印我的用户看起来像这样:
1425436064.0User(key=Key('User', 5171003185430528), active=True, email=u'NULL', name=u'NULL', others=[UserOther(other_data=u'0', other_type=u'job'), UserOther(other_data=u'0', other_type=u'times_worked'), UserOther(other_data=u'0', other_type=u'times_opened')], updated_at=datetime.datetime(2015, 3, 6, 10, 35, 24, 838078))
但是如果我打印作业变量就是
1425436759.0None
答案 0 :(得分:2)
您误解了如何查询结构化属性。 UserOther实体并非独立于相关用户实体,而是您需要查询的内容。
documentation确切地解释了如何执行此操作,但总的来说,您可以这样做:
job = User.query(User.others.other_type == "job").get()
答案 1 :(得分:0)
我要做的是获取用户(通过id),然后在代码中过滤“其他人”:
user = User.get_by_id(user_key_id)
for other in user.others:
if other.other_type == 'job':
print other.other_data # do edits