Reputation: 97
I have been working on a project using Google App Engine. I have been setting up users and have to check if a username is taken yet.
I used the following code to try to test whether it is taken or not
usernames = db.GqlQuery('select username from User')
taken = username in usernames
This never caught duplicate usernames. I tried a few variants of this on the GQL query line. I tried using .get()
which caused an error because it returned something that wasn't iterable. I also tried putting list()
around the request, which returned the same error. I tried writing the value of usernames
but never got any response. If it returns a query instance, then is there any way to turn it into a list or tuple?
Upvotes: 0
Views: 185
Reputation: 12986
For starters you should revisit the docs https://cloud.google.com/appengine/docs/python/datastore/gqlqueryclass?hl=en
db.GqlQuery('select username from User')
is calling a constructor not a function so it returns an instance of a GqlQuery
object. See docs referred to above.
Secondly what you are doing will never work reliably due to eventual consistancy . Please read https://cloud.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency to understand why.
Lastly you are starting out with appengine, so move away from db
and use ndb
unless you have a significant existing code base.
Upvotes: 1