2013-01-14 282 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') 

如果您使用位置參數,第一個參數被解釋爲父母,這就需要將型型號或關鍵的。

相關問題