如何在google appengine/python上为模型大规模分配属性?
我知道我可以在创建类时使用构造函数进行批量分配:如何在google appengine/python上为模型大规模分配属性?,python,google-app-engine,mass-assignment,Python,Google App Engine,Mass Assignment,我知道我可以在创建类时使用构造函数进行批量分配: attributes = {'name':'John', 'surname':'Smith', ...} record = Model(**attributes) 但是,当我有现有记录时,如何分配属性 record = .... record.???(**attributes) 谢谢 您只需分配给属性 mymodel = MyModel() mymodel.name = 'John' mymodel.surname = 'Smith' 如果
attributes = {'name':'John', 'surname':'Smith', ...}
record = Model(**attributes)
但是,当我有现有记录时,如何分配属性
record = ....
record.???(**attributes)
谢谢 您只需分配给属性
mymodel = MyModel()
mymodel.name = 'John'
mymodel.surname = 'Smith'
如果您想实际执行初始化器所做的操作,那么可以使用setattr
attributes = {'name':'John', 'surname':'Smith'}
record = Model()
for k, v in attributes.iteritems():
setattr(record, k, v)
见:
从那里:
应用程序通过实例化子类来创建新的数据实体
模型类的。可以使用指定实体的属性
实例的属性,或作为
构造器
s = Story()
s.title = "The Three Little Pigs"
s = Story(title="The Three Little Pigs")