Hello everyone,
I get form data in my view - which should overwrite an existing data record in the database.
It would be very time-consuming to describe in detail why I use this - perhaps not nice - solution.
To my question:
In the loop I have the following data:
- “project” to identify the correct data set.
- “model_data_fiel”, the exact designation of the database field. (e.g. “url_address”)
- “new_value” - the new value for this database field.
“update_model.model_data_field” doesn’t work of course - “update_model.url_adresse” would be correct. But how can I dynamically access a database field - is there a syntax for this?
for data in form_data:
project = data.split("?%§?")[0]
model_data_field = data.split("?%§?")[1]
new_value = data.split("?%§?")[2]
update_model = ProjectData.objects.get(project=project)
update_model.model_data_field = new_value
update_model.save()