Sunday, 15 August 2010

Google app engine's ndb: using id or key name? -



Google app engine's ndb: using id or key name? -

this google app engine's ndb. according observations, if create entity without providing key, entity have integer, starting 1 , beingness incremental, in id field shown in datastore viewer. however, if create entity providing string id, entity have string in key name field. example:

model:

class member(ndb.model): ...

program:

member1 = member() # --> id 1, key name none member2 = member(id='abc') # --> id empty, key name 'abc'

in addition, in html file, if utilize

<input ... name="id" value={{member1.key.id}} ... />

as parameter homecoming server-side programme (python), none of next 2 statements work member1:

member.get_by_id(self.request.get('id')) member1 = member.get_by_id(int(self.request.get('id')))

however, next html , programme codes:

<input ... name="id" value={{member2.key.id}} ... /> member2 = member.get_by_id(self.request.get('id'))

will work member2.

there seems no problem entities created providing string id's (i.e., member2). same doesn't work member1. questions: a) observations correct? b) how fetch member1 using get_by_id()?

a) correct, though should able member1 through sec method showed. can't verify integer ids start @ 1 , incremental. i've seen mixed results in regards that.

b) member1 = member.get_by_id(int(self.request.get('id'))) should work

you seek using key.urlsafe() dont have worry key id conversions:

<input ... name="id" value={{member.key.urlsafe()}} ... />

member_key = self.request.get('id') fellow member = ndb.key(urlsafe=member_key).get()

google-app-engine

No comments:

Post a Comment