我实际上是在python中编写Web服务代码,而我无法将值存储到KeyProperty值中。我有一对多的关系(Item - > Category)。我想将我的类别ID存储在Item类的category字段中。这是我的代码:
class Item(ndb.Model):
name = ndb.StringProperty(required=True, indexed=True)
desc = ndb.StringProperty(required=True)
price = ndb.FloatProperty(required=True)
category = ndb.KeyProperty(kind='Categorie', repeated=True)
class Category(ndb.Model):
name = ndb.StringProperty(required=True, indexed=True)
class ItemHandler(webapp2.RequestHandler):
def insert(self, item_ID):
try:
keyItem = ndb.Key('Item', item_ID)
item = keyItem.get()
if item is None:
status = 201
item = Item(key=keyItem)
else:
status = 200
dict = json.loads(self.request.body)
keyCategory = ndb.Key('Category', dict['category'])
cat = ndb.Key('Categorie', item_dict_in['categorie']).get()
# If data doesn't exist, I insert it
if cat is None:
cat.name = dict['categorie']
me.put();
# continue, all work fine after to write the output.
有人可以帮助我吗?
Tancks