1

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

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

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

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
4

1 回答 1

1

我根据 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

还更新了播放列表 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

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

于 2015-01-20T17:51:26.033 回答