Question

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!

Was it helpful?

Solution

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")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top