我的项目中有以下模型和各自的方法:
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 查询为何总是返回一个空列表。 Dev app引擎服务(本地)和托管应用引擎服务(远程)都返回空列表。
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:
[
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))
]
有人能指出我正确的方向吗?
答案 0 :(得分:1)
您正在混合使用字符串和int ID:您的Users实体具有int id,但UsersPhotos parent的ID部分是字符串。
在insert_from_form方法中创建user_key时,应该转换值:
user_key = ndb.Key(Users, int(data.userId))