python - Save data in datastore -
if have class , list (but in case 30 values):
class abc(ndb.model): x = ndb.stringproperty() y = ndb.stringproperty() z = ndb.stringproperty() var = {'x': 'oi', 'y': 'tim', 'z': 'vivo'}
and want save values in datastore, there way gave use or while loop when calling class save values without need set 1 value eg:
xabc = abc(x = var['x'], y = var['y'], z = var['z']) xabc.put()
i see no "list" here described in question, don't know if i've interpreting question correctly.
i think you're trying ask: how initialize abc
instance, given dictionary dynamic (but limited) set of key/value pairs?
if understand intent is, should able simple loop on dictionary's key/value pairs, , use setattr()
push keys entity.
xabc = abc() key, value in var.iteritems(): setattr(xabc, key, value)
alternative approach: can directly use constructor, feeding in keyword arguments dictionary using **
splice in keyword arguments:
xabc = abc(**var)
do have limits on kind of keys in dictionary? nice if knew front these are. dictionary coming from?
Comments
Post a Comment