gpt4 book ai didi

Python endpoints_proto_datastore 通过 KeyProperty 查询

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:56 26 4
gpt4 key购买 nike

我刚刚开始使用 AppEngine 并使用 endpoints_proto_datastore 库。我的问题:当我尝试列出特定播放列表的所有 PlaylistItems 时,查询路径中的“播放列表”参数似乎被忽略。我只是从所有播放列表中检索所有播放列表项。

我做错了什么?我无法找到任何具有两个模型的示例,其中模型通过 KeyProperty 关联并通过路径元素查询。我也不明白为什么我在数据存储区中看不到 PlaylistItem.playlist(除非它是 key 的一部分)。

这些是相关的模型和方法:

from endpoints_proto_datastore.ndb import EndpointsAliasProperty
from endpoints_proto_datastore.ndb import EndpointsModel

class Playlist(EndpointsModel):
"""Models an individual Playlist."""
# fixed order
_message_fields_schema = ('id', 'user', 'title', 'private', 'created')
# actual stored information
user = ndb.StringProperty(required=True, indexed=True)
title = ndb.StringProperty(required=True, indexed=False)
private = ndb.BooleanProperty(required=False, default=True, indexed=False)
created = ndb.DateTimeProperty(required=False, auto_now_add=True, indexed=False)


class PlaylistItem(EndpointsModel):
"""Models a playlist item (appid)"""
# fixed order
_message_fields_schema = ('appid', 'inserted')
# actual stored information
appid = ndb.StringProperty(required=True, indexed=False)
inserted = ndb.DateTimeProperty(required=False, auto_now_add=True, indexed=True)
playlist = ndb.KeyProperty(required=True, kind=Playlist, indexed=True)

def PlaylistSet(self, value):
playlist = ndb.Key(Playlist, value)

@EndpointsAliasProperty(setter=PlaylistSet, required=True)
def playlist(self):
playlist

以及相关方法:

    @PlaylistItem.method(path='playlist/{playlist}/items', http_method='POST', name='playlist.items.put',
request_fields=('playlist','appid'))
def PlaylistItemPut(self, PlaylistItem):
PlaylistItem.put()
return PlaylistItem

@PlaylistItem.query_method(path='playlist/{playlist}/items', name='playlist.items.list',
query_fields=('playlist',),)
def PlaylistItemList(self, query):
return query # problem, I just retrieve all PlaylistItems

最佳答案

我根据 Alex 的建议更改了代码。看来我遇到的最重要的错误是没有使用 PlaylistSet 方法中的 self.playlist 。后来我遇到了亚历克斯指出的命名冲突。以下是所有更改后的工作代码:

class PlaylistItem(EndpointsModel):
"""Models a playlist item (appid)"""
# fixed order
_message_fields_schema = ('appid', 'inserted')
# actual stored information
appid = ndb.StringProperty(required=True, indexed=False)
inserted = ndb.DateTimeProperty(required=False, auto_now_add=True, indexed=True)
playlist = ndb.KeyProperty(required=True, kind=Playlist, indexed=True)

def PlaylistSet(self, value):
self.playlist = ndb.Key(Playlist, value) # FIX: MUST use self.playlist here!

@EndpointsAliasProperty(setter=PlaylistSet, required=True)
def myplaylist(self): # renamed the method
return playlist # return the playlist

还更新了 Playlist API 方法以使用新的 myplaylist 名称:

    @PlaylistItem.method(path='playlist/{myplaylist}/items', http_method='POST', name='playlist.items.put',
request_fields=('myplaylist','appid')) # renamed request field
def PlaylistItemPut(self, PlaylistItem):
PlaylistItem.put()
return PlaylistItem

@PlaylistItem.query_method(path='playlist/{myplaylist}/items', name='playlist.items.list',
query_fields=('myplaylist',),) # renamed request field
def PlaylistItemList(self, query):
return query

我还可以在数据存储查看器中看到播放列表“列”(如您所料,之前未设置)。

关于Python endpoints_proto_datastore 通过 KeyProperty 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28035185/

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