Reputation: 364
First off, I'm relatively new to Google App Engine, so I'm probably doing something silly.
I want the username to be set as key in model User
class User(db.Model):
#username is key
password = db.StringProperty(required = True)
type = db.StringProperty(required = True)
approved = db.BooleanProperty(required = True)
To insert i do this
user = User(key_name = self.request.get('username'), password = password, type = type, approved = False)
user.put()
I believe that when you set key_name manually it should be exactly what you set it to be but when i query user modle
users = db.GqlQuery("SELECT * FROM User")
for(user in users):
self.response.write(user.key())
I got the output as agxkZXZ-dmhvc3RlbDNyEQsSBFVzZXIiB2JodXNoYW4M
Please someone help!!
Upvotes: 0
Views: 66
Reputation: 4178
Perhaps self.request.get('username')
is returning Null or None, and that results in the Datastore generating a default entry. According to the Users Service documentation it might need users.get_current_user().nickname()
instead. Check by logging the values.
As Tim says you retrieve the name from the key using user.key().name()
.
Upvotes: 0
Reputation: 12986
To start with you should read the docs on the Key class https://developers.google.com/appengine/docs/python/datastore/keyclass and how keys a structured - https://developers.google.com/appengine/docs/python/datastore/#Python_Kinds_keys_and_identifiers
Any way to your problem, note that the output of self.response.write(user.key())
is giving you the string agxkZXZ-dmhvc3RlbDNyEQsSBFVzZXIiB2JodXNoYW4M
which is correct behaviour.
This is a URL safe form of the key which encodes all artifacts that make up the key.
This means you can round trip
user.key() = db.Key(encoded=str(user.key())
This allows you to use keys as part of URL. Whether that's wise or not is another discussion.
If you want to just show the name you used as the key_name
then the docs for the Key
class show you that the method name()
will return the name.
As in user.key().name()
or you could use id_or_name
method which does what the name implies.
Upvotes: 2