gpt4 book ai didi

python - 祖先的 NDB 查询返回的不是实体

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

我的项目中有以下模型和各自的方法:

class Users(ndb.Expando):
"""
flefu users, identified by email.
user_gender --> 0 = male; 1 = female; 2 = other; 3 = unknown
"""
join_date = ndb.DateTimeProperty(auto_now_add=True)
user_name = ndb.StringProperty(required=True)
user_email = ndb.StringProperty(required=True)


class UsersPhotos(ndb.Model):
"""
property to processes a blob upload steps
Users child.
"""

upload_date = ndb.DateTimeProperty(auto_now_add=True)
photo_key = ndb.BlobKeyProperty(required=True)
photo_serving_url = ndb.StringProperty(required=True)

@classmethod
def insert_from_form(cls, data):
user_key = ndb.Key(Users, data.userId)

photo_details = cls(
parent=user_key,
user=user_key,
photo_key=data.photoKey,
photo_serving_url=data.photoServingUrl
)

photo_details.put()
photo_details.status = "200"
photo_details.parent = user_key.id()

return photo_details

@classmethod
def get_photos_by_users(cls, data):
users = Users.query().fetch(20)

for user in users:

logging.info(str(user.key))
photo_data = cls.query(ancestor=user.key).fetch()
logging.info(str(photo_data))

photos = cls.query().fetch(10)
logging.info(str(photos))

我不确定为什么 photo_data 查询总是返回空列表。开发应用引擎服务(本地)和托管应用引擎服务(远程)都会返回空列表。

Users 和 UsersPhotos 类型具有以下实体:

用户:

Users(key=Key('Users', 5629499534213120), join_date=datetime.datetime(2014, 7, 26, 4, 40, 11, 840853), user_email=u'user@domain.com', user_name=u'hidden')

用户照片:

[
UsersPhotos(key=Key('Users', '5629499534213120', 'UsersPhotos', 6192449487634432), photo_key=datastore_types.BlobKey('encoded_gs_file:YXBwX2RlZmF1bHRfYnVja2V0L2Zha2UtdzZSYkpZS1RnTlpkY3JKRVI3dUU2UT09'), photo_serving_url=u'http://127.0.0.1:8080/_ah/img/encoded_gs_file:YXBwX2RlZmF1bHRfYnVja2V0L2Zha2UtdzZSYkpZS1RnTlpkY3JKRVI3dUU2UT09=s300', upload_date=datetime.datetime(2014, 7, 26, 4, 40, 35, 455648)),
UsersPhotos(key=Key('Users', '5629499534213120', 'UsersPhotos', 6473924464345088), photo_key=datastore_types.BlobKey('encoded_gs_file:YXBwX2RlZmF1bHRfYnVja2V0L2Zha2UtcmZiYU5WNEFSVnZheG81aGQ5dXpNdz09'), photo_serving_url=u'http://127.0.0.1:8080/_ah/img/encoded_gs_file:YXBwX2RlZmF1bHRfYnVja2V0L2Zha2UtcmZiYU5WNEFSVnZheG81aGQ5dXpNdz09=s300', upload_date=datetime.datetime(2014, 7, 26, 4, 41, 12, 322560))
]

有人能指出我正确的方向吗?

最佳答案

您正在混合字符串和 int id:您的 Users 实体有一个 int id,但 UsersPhotos 父级的 ID 部分是一个字符串。

当您在 insert_from_form 方法中创建 user_key 时,您应该转换该值:

user_key = ndb.Key(Users, int(data.userId))

关于python - 祖先的 NDB 查询返回的不是实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24971853/

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