0

I know I can use constructor to mass assign when creating the class:

attributes = {'name':'John', 'surname':'Smith', ...}
record = Model(**attributes)

but how can I assign the attributes when I have existing record?

record = ....
record.???(**attributes)

Thanks!

hakunin
  • 4,041
  • 6
  • 40
  • 57

1 Answers1

2

You just assign to the attribute...

mymodel = MyModel()
mymodel.name = 'John'
mymodel.surname = 'Smith'

If you wanted to in effect do what the initialiser does, then you can use setattr

attributes = {'name':'John', 'surname':'Smith'}
record = Model()

for k, v in attributes.iteritems():
    setattr(record, k, v)

See: https://developers.google.com/appengine/docs/python/datastore/modelclass#Model

and from there:

An application creates a new data entity by instantiating a subclass of the Model class. Properties of an entity can be assigned using attributes of the instance, or as keyword arguments to the constructor.

s = Story()
s.title = "The Three Little Pigs"

s = Story(title="The Three Little Pigs")
Jon Clements
  • 138,671
  • 33
  • 247
  • 280