[Answer]-Iterating through a data list and edit an object's attribute based on the list

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])

Source: How to set django model field by name?

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.

👤pcurry

Leave a comment