由KeyProperty进行的Python endpoints_proto_datastore查询

时间:2015-01-19 23:17:20

标签: python google-app-engine google-cloud-datastore app-engine-ndb endpoints-proto-datastore

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

我做错了什么?我无法找到两个模型的任何示例,其中模型通过KeyProperty相关并通过path-element查询。我也不明白为什么我在数据存储区中看不到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

1 个答案:

答案 0 :(得分:1)

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

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

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