'How to dynamically set column value in flask_sqlalchemy
I have a route which may take in a variating number of request parameters. For example, I have an array of possible params which MAY be present in my data dictionary:
possible_params = ["param_1", "param_2"...]
All of the parameters in the possible_params array are columns in a db.Model instance (table). Lets call the table Users.
What I want to do is basically loop through each parameter, check if it exists in the data dictionary and set the attribute of Users instance accordingly.
That is what puzzles me - how do I set the value of a class instance attribute when the name of that attribute is a variable?
I could go with the traditional approach of
if "param_1" in data:
Users.param_1 = data["param_1"]
if "param_2" in data:
Users.param_2 = data["param_2"]
but that seems a little... caveman-ish.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
