1
votes

I'm using google app engine and I'm trying to insert a entity/table using the code:

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()

but I get this error:

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

I'm following this doc https://developers.google.com/appengine/docs/python/datastore/entities and I don't see where I'm wrong.

2

2 Answers

2
votes

When you create a model in that way, you need to use keyword arguments for all attributes of your model. Here is a snippet of the __init__ signature from db.Model, from which your Tu model inherits:

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

When you say a = Tu('teste', 'bla bla bla bla'), since you aren't providing keyword arguments and are instead passing them as positional arguments, teste is assigned to the parent argument in the __init__ (and bla bla bla bla to the key_name) and since that argument needs an object of type Model (which I'm assuming you don't have), you get that error. Assuming you are instead trying to add those items as title and presentation, you would say (as @DanielRoseman already succinctly stated :) ):

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

The docs you link to all use keyword arguments:

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

If you use positional arguments, the first argument is interpreted as the parent, which needs to be of type Model or Key.