SQLAlchemy commit changes to object modified through __dict__

后端 未结 2 1972
旧巷少年郎
旧巷少年郎 2020-11-30 10:47

I am developing a multiplayer game. When I use an object from inventory, it should update the user creature\'s stats with the values of the attributes of an object.

2条回答
  •  感情败类
    2020-11-30 10:58

    Don't use __dict__. Use getattr and setattr to modify attributes by name:

    for attribute in obj.attributes:
        setattr(cur_creature,str(attribute.Name), getattr(cur_creature,str(attribute.Name)) + attribute.Value)
    

    More info:

    setattr

    getattr

提交回复
热议问题