I have a dictionary that I would like to iterate through and copy the key-value pairs into an object in Python. The dictionary is POST, and the object is a Model (in Django, perhaps Django has better way to do this).
In PHP, I'd be able to use variable assignments:
foreach($post as $key => $value) {
$my_model->$key = $value;
}
And in Javascript I could treat the object with array assignments:
for(var key in post) {
my_model[key] = post[key];
}
However, I can't seem to do so in Python. The only way I've seen is by using the objects __dict__
property, and it feels ever so slightly dirty. Plus it can raise KeyErrors.