开发者

How to create dynamic fields in Google App Engine expando class?

开发者 https://www.devze.com 2023-01-24 10:56 出处:网络
I have a db expando class called widget. I\'m passing in a json string开发者_如何学C and converting it to a dict and then adding it to the datastore.

I have a db expando class called widget.

I'm passing in a json string开发者_如何学C and converting it to a dict and then adding it to the datastore.

My question is how can I loop through my dict to create dynamic fields.

widget = Widget.get_by_key_name(key_name)
widget.name = self.request.get('wname')
fields = simplejson.loads(self.request.get('wcontents'))
for k,v in fields.iteritems():
  widget.k = v

This renders "k" as my field name as oppose to the k value in the dict.


The syntax widget.k references the attribute k on object widget. To dynamically choose which attribute you set, use the built-in setattr method:

setattr(widget, k, v)

Dynamically setting an attribute like this will create the field on that particular entity.


Try:

for k, v in fields.items()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号