To build a dynamic user update route on Flask, I have iterate over the user Flask SQLAlchemy object using the dunder __dict__
:
parameters = ['name'] # Valid parameters after the Regex filter was applied
for parameter in parameters:
user.__dict__[parameter] = request.form.get(parameter)
I have done this to avoid the usage of ifs
. To ensure that only valid parameters are present in parameters
, I have applied a Regex pattern that filters the valid parameters received in the request
for the user route, and I have documented this aspect on the doc string.
I'm asking if iterate over a Flask SQLAlchemy object using __dict__
is it a bad practice because if I print the user.__dict__
, I receive all parameters, even those that aren't on the Regex filter, i.g, password, date created, etc; and should never be updated by this route.
I have found another approach that uses get all columns in SQLAlchemy, but I think that at the end its similar to the approach that I'm using...
Obs: the implemented route can update specific attributes from user or all of them, using the same route