Google appengine-db.key()
Hi am going through the docs of GAE and needed a small clarification. If I have my db model something like this:-
class Phone(Model):
phone_name = db.StringProperty()
r = Phone(Nokia, key_name='first')
r.put()
Now if I have to retrieve this entity but I dont know the key, can I construct the key like this:
k=db.Key('Phone','first')
and once the key is constr开发者_StackOverflow社区ucted, can the entity be retrieved like this:-
r=db.get(k)
You're close. The only major difference is that you have to pass the actual class instead of a string representing the class name, and that you have to use the Key.from_path()
factory method rather than the default constructor:
class Phone(Model):
phone_name = db.StringProperty()
r = Phone(phone_name='Nokia', key_name='first')
r.put()
k = db.Key.from_path('Phone', 'first')
r = db.get(k)
On the whole, however, I have found that relying on auto-generated IDs is usually a better solution than specifying your own key names. Is there a particular reason you're doing the latter?
精彩评论