1👍
✅
You can do the following (with iteritems()
and setattr()
):
for key, value in data.iteritems():
setattr(property_selected, key, value)
But you should probably limit the fields that are allowed to be edited:
for key, value in data.items():
if key in ['foo_field', 'bar_field']:
setattr(property_selected, key, value)
0👍
To set an attribute by name you can use the setattr method of a django model instance
setattr(property_selected, d, data[d])
0👍
Use the builtin function setattr.
for d in data:
setattr(property_selected, d, data[d])
There are a number of answers using setattr that are illuminating reading.
- [Answer]-CSRF verification failed. Request aborted. in django ajax post
- [Answer]-Circular dependency? Reverse for 'home' with arguments '()' and keyword arguments '{}' not found
Source:stackexchange.com