TheSneak TheSneak - 2 months ago 13
Python Question

Google App Engine Datastore .put() and .get() methods not working?

I'm trying to select an entity from one table of the datastore, get some information out of it, and then put that information into an entity in a different table. The put() and get() methods don't seem to be working though and I can't figure out why.

aName = 'Hello'
q = firstTable.all()
q.filter("date =", datetime.date.today())
dateInfo = q.get() #get method here works fine
p = db.GqlQuery("SELECT * FROM secondTable WHERE name = '%s'" % aName)
result = p.run(limit = 1) #here, there's an error if I use get(), but no error if I use run()

result.firstProperty = dateInfo.firstProperty
result.secondProperty = dateInfo.secondProperty

result.put() #this line does not work


Any idea what could be wrong here? Could a problem in the entities themselves cause this behavior? Particularly perplexing to me is that the .run() method works for p, but the .get() does not work. What could cause that?

The error message I get in the log says:
'_QueryIterator' object has no attribute 'put' Traceback (most recent call last): File "/base/data/home/runtimes/python27/python27_lib/versions/thir

The way I know that certain lines don't work is that, when I comment out those particular lines, the page loads and doesn't throw the above error.

If I try to use .get() instead of .run(), then it gives this error:
raise BadValueError('Property %s is required' % self.name)
BadValueError: Property info is required

Answer

You error is because run causes the query to be run, returning an iterator which you need to iterate over the results. It doesn't directly return an entity.

In your case you are trying to update the iterator itself and then call put()

which of course fails.

Even with a limit of 1, you need to iterate.

You should be using get, however you point out you get an error.

error: raise BadValueError('Property %s is required' % self.name) BadValueError: Property info is required

This will be because you have changed a property in your model info and added required=True but you have some old data in the datastore that has no value for info

You problem has a lot to do with trying/changing too many things at once without really understanding what is going on. I suggest you go back and read the docs on queries and their methods, and remove required=True from your info property in the model until such time as you can update all the entities so that info has a value.

Comments