1

私はグーグルアプリエンジンを使用していて、コードを使用してエンティティ/テーブルを挿入しようとしています:

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)

。。。

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

しかし、私はこのエラーを受け取ります:

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

このドキュメントhttps://developers.google.com/appengine/docs/python/datastore/entitiesをフォローしていますが、どこが間違っているのかわかりません。

4

2 に答える 2

2

この方法でモデルを作成するときは、モデルのすべての属性にキーワード引数を使用する必要があります。これは、モデルが継承するからの__init__署名のスニペットです。db.ModelTu

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割り当てられ、その引数はタイプのオブジェクトを必要とするので(私はあなたがありません)、そのエラーが発生します。代わりに、これらのアイテムをととして追加しようとしていると仮定すると、次のようになります(@DanielRosemanがすでに簡潔に述べているように:)):parent__init__bla bla bla blakey_nameModeltitlepresentation

a = Tu(title='teste', presentation='bla bla bla bla')
于 2013-01-14T16:46:50.900 に答える
2

リンクするドキュメントはすべてキーワード引数を使用します。

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

位置引数を使用する場合、最初の引数は親として解釈され、モデルまたはキーのタイプである必要があります。

于 2013-01-14T16:45:45.747 に答える