Reputation: 1
If I have a class and a list (But in my case are about 30 values):
class ABC(ndb.model):
x = ndb.StringProperty()
y = ndb.StringProperty()
z = ndb.StringProperty()
var = {'x': 'oi', 'y': 'tim', 'z': 'vivo'}
and I want to save those values in the datastore, is there a way gave use a for or while loop when calling the class to save the values without which I need to set one value eg:
xabc = ABC(x = var['x'], y = var['y'], z = var['z'])
xabc.put()
Upvotes: 0
Views: 92
Reputation: 12033
I see no "list" here as described in your question, so I don't know if I've interpreting your question correctly.
I think you're trying to ask: how do I initialize an ABC
instance, given a dictionary with a dynamic (but limited) set of key/value pairs?
If I understand what your intent is, then you should be able to do a simple loop over the dictionary's key/value pairs, and use setattr()
to push those keys into your entity.
xabc = ABC()
for key, value in var.iteritems():
setattr(xabc, key, value)
Alternative approach: you can directly use the constructor, feeding in the keyword arguments from the dictionary using **
to splice in keyword arguments:
xabc = ABC(**var)
Do you have any limits on what kind of keys will be in your dictionary? It would be very nice if you knew up front what these are. Where is the dictionary coming from?
Upvotes: 1