0

我正在尝试插入 Category 对象,但我喜欢使用 Store ID 将 Category 链接到 Store。ID 来自端点原型数据存储。我怎样才能做到这一点?

实际上,要保存类别,API 需要类别名称和存储密钥。

这是我的api方法:

@Category.method(user_required=True, 
                request_fields=('name'), 
                path='{store}/categories', 
                http_method='POST', 
                name='categories.insert')
def insertCategory(self, category):
    """
    Insert a category in a existing store.
    """
    category.put()
    return category

这是我的类别模型:

class Category(EndpointsModel):
    _message_fields_schema = ('id', 'name', 'store')
    name = ndb.StringProperty(required=True)
    store = ndb.KeyProperty(Store)

这是我的商店模型:

class Store(EndpointsModel):
    _message_fields_schema = ('id', 'name')
     name = ndb.StringProperty(required=True)  
4

1 回答 1

0

假设您已经有一个要包含在 Category 实体中的 Store 实体,请确保使用 Store 键实体而不是 id。您可以在数据存储查看器(旧版本)中找到关键实体。

如果您像这样添加 entityKey 默认属性,则可以在实体的查询中获取键。

class Store(EndpointsModel):
    _message_fields_schema = ('id', 'entityKey', 'name')
    name = ndb.StringProperty(required=True)
于 2015-01-28T05:24:12.347 回答