python - Why does put() not work when used with indexed element in Query? -
i have issue put()
not appear work if access model class indexed member of query
directly; however, when explicitly extract class query
seems work fine. why this?
this code works:
class record: field = db.stringproperty() rs = record.all().filter('name = ', name_str) if rs.count() == 1: # assume 1 record in return... r = rs[0] r.field = some_value r.put()
and code doesn't (and not raise errors)
class record: field = db.stringproperty() rs = record.all().filter('name = ', name_str) if rs.count() == 1: # assume 1 record in return... rs[0].field = some_value rs[0].put()
every time index query this, performs query on again, fetches relevant result, decodes it, , returns you. in second snippet, modify 1 instance of entity, discard it, fetch , store (unmodified) second copy.
generally, should avoid indexing queries - call .get()
or .fetch()
instead.
for same reason, should avoid using .count()
possible, since requires running query. if want 1 result, call .get()
; if need more, call .fetch()
, count number of results returned.
Comments
Post a Comment