Question

Edit: Sorry I didn't clarify this, it's a Google App Engine related question.

According to this, I can give db.put() a list of model instances and ask it to input them all into the datastore. However, I haven't been able do this successfully. I'm still a little new with Python, so go easy on me

list_of_models = []
for i in range(0, len(items) - 1):
    point = ModelName()

    ... put the model info here ...

    list_of_models.append(point)

db.put(list_of_models)

Could anyone point out where I'm going wrong?

Was it helpful?

Solution

Please define what you mean by "going wrong" -- the tiny pieces of code you're showing could perfectly well be part of an app that's quite "right". Consider e.g.:

class Hello(db.Model):
  name = db.StringProperty()
  when = db.DateTimeProperty()

class MainHandler(webapp.RequestHandler):

  def get(self):
    self.response.out.write('Hello world!')
    one = Hello(name='Uno', when=datetime.datetime.now())
    two = Hello(name='Due', when=datetime.datetime.now())
    both = [one, two]
    db.put(both)

this does insert the two entities correctly each time that get method is called, for example if a sample app continues with:

def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()

as in a typical "hello world" app engine app. You can verify the correct addition of both entities with the datastore viewer of the sdk console, or of course by adding another handler which gets the entities back and shows them, etc etc.

So please clarify!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top