Google App Engine - 从重复的 StructuredProperty 中获取
Google App Engine - Get from repeated StructuredProperty
我有以下结构:
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")的字符串来获取并能够编辑该信息。我试过使用 ancestor 参数,但也许我没有做对。
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
您误解了如何查询结构化属性。 UserOther 实体不是独立存在的,它是相关 User 实体的一部分,因此这就是您需要查询的内容。
documentation 详细说明了如何执行此操作,但总而言之,您将执行以下操作:
job = User.query(User.others.other_type == "job").get()
我要做的是获取用户(通过 id),然后在代码中过滤 'others':
user = User.get_by_id(user_key_id)
for other in user.others:
if other.other_type == 'job':
print other.other_data # do edits
我有以下结构:
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")的字符串来获取并能够编辑该信息。我试过使用 ancestor 参数,但也许我没有做对。
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
您误解了如何查询结构化属性。 UserOther 实体不是独立存在的,它是相关 User 实体的一部分,因此这就是您需要查询的内容。
documentation 详细说明了如何执行此操作,但总而言之,您将执行以下操作:
job = User.query(User.others.other_type == "job").get()
我要做的是获取用户(通过 id),然后在代码中过滤 'others':
user = User.get_by_id(user_key_id)
for other in user.others:
if other.other_type == 'job':
print other.other_data # do edits