2013-01-14 662 views
1

我使用谷歌应用程序引擎,我尝试使用的代码中插入一个实体/表:创建App Engine数据存储实体

class Tu(db.Model): 
    title = db.StringProperty(required=True) 
    presentation = db.TextProperty(required=True) 
    created = db.DateTimeProperty(auto_now_add=True) 
    last_modified = db.DateTimeProperty(auto_now=True) 

。 。 。

TypeError: Expected Model type; received teste (is str) 

我下面这个文档https://developers.google.com/appengine/docs/python/datastore/entities,我看不出我是错的:

a = Tu('teste', 'bla bla bla bla') 
     a.votes = 5 
     a.put() 

,但我得到这个错误。

回答

2

以这种方式创建模型时,需要为模型的所有属性使用关键字参数。下面是从db.Model__init__签名,从中你Tu模型继承的一个片段:

def __init__(self, 
       parent=None, 
       key_name=None, 
       _app=None, 
       _from_entity=False, 
       **kwds): 
    """Creates a new instance of this model. 

    To create a new entity, you instantiate a model and then call put(), 
    which saves the entity to the datastore: 

     person = Person() 
     person.name = 'Bret' 
     person.put() 

    You can initialize properties in the model in the constructor with keyword 
    arguments: 

     person = Person(name='Bret') 

    # continues 

当你说:a = Tu('teste', 'bla bla bla bla'),因为你没有提供的关键字参数,并改为将它们作为位置参数,teste分配到__init__(和bla bla bla blakey_name)中的parent参数,并且由于该参数需要Model(我假设您没有)类型的对象,因此会出现该错误。假设你是不是尝试添加这些项目为titlepresentation,你会说(如@DanielRoseman已经熏陶中:)):

a = Tu(title='teste', presentation='bla bla bla bla') 
2

您链接到所有使用的关键字参数文档:

a = Tu(title='tests', presentation='blablablah') 

如果您使用位置参数,第一个参数被解释为父母,这就需要将型型号或关键的。

相关问题