Python 在数据存储中插入数据-应用程序引擎-一对多

Python 在数据存储中插入数据-应用程序引擎-一对多,python,google-app-engine,datastore,Python,Google App Engine,Datastore,我是GAE和python的初学者。我在理解如何插入具有一对多关系的数据时遇到困难,我对自己正在做的事情不太了解 我正在尝试创建一个清单应用程序。基本上,我想在每个类别下插入产品 我的数据库模型如下 class Category(db.Model): category = db.StringProperty() date = db.DateTimeProperty(auto_now_add=True) class Product(db.Model): ref_catego

我是GAE和python的初学者。我在理解如何插入具有一对多关系的数据时遇到困难,我对自己正在做的事情不太了解

我正在尝试创建一个清单应用程序。基本上,我想在每个类别下插入产品

我的数据库模型如下

class Category(db.Model):
    category = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)

class Product(db.Model):
    ref_category = db.ReferenceProperty(Category)
    name = db.StringProperty()
我能够在数据存储中插入类别,但每个类别的产品是我遇到问题的地方

我有一个如下的表格,我不确定这是否也是正确的方式。在隐藏输入中使用键插入也可以吗

<form action="/addproduct" method="post">
        <div><label>Product Name:</label><br /><input type="name" name="name" /></div>
        <div><input type="hidden" name="ref_category" value="{{selected_category.key()}}" /></div>
        <input type="submit" value="Add Candidate">
        </form>

我希望有人能帮助我理解何时提供解决方案

您正试图将字符串设置为ReferenceProperty,因为self.request.get返回类型为字符串。
Product
的“
ref\u category
”字段是一个
db.ReferenceProperty
,它接受一个
db.Key
或一个
category
对象,但您试图将其设置为字符串

您可以这样做:

def post(self):
    product = Product()
    product.ref_category = db.get(self.request.get('ref_category'))
    product.put()

无需点击数据存储即可获得实际对象:
db.Key(…)
就可以了。哇,太好了!非常感谢Nijin和Daniel!如果您正在启动一个新项目或学习应用程序引擎,那么我建议您开始使用ndb。
def post(self):
    product = Product()
    product.ref_category = db.get(self.request.get('ref_category'))
    product.put()