0

I am trying to gather the values of a column from table. I used SQLAlchemy to get all the objects, and then I do a loop to get all the results for a specific column - column_name. However, I get the column name from the client (browser), so it comes in as a string. As a results, I get the error: AttributeError: 'DataTable' object has no attribute 'column_name'. How do I convert the value of column_name into the correct type?

def chart_data():
    column_name = request.args.get('id')
    person_id = 1
    results = DataTable.query.filter_by(person_id = person_id).all()
    alist = []
    for x in results:
        alist.append(float(round(x.column_name, 4)))
    return jsonify({"results" : alist})
Dave
  • 127
  • 1
  • 8
  • Possible duplicate of [What is getattr() exactly and how do I use it?](https://stackoverflow.com/questions/4075190/what-is-getattr-exactly-and-how-do-i-use-it) – mkrieger1 Dec 13 '17 at 16:58
  • Or rather https://stackoverflow.com/questions/6305061/get-an-object-attribute – mkrieger1 Dec 13 '17 at 17:02
  • did you try `x[column_name]` ? Now it is like `x[ "column_name" ]`. – furas Dec 13 '17 at 17:30
  • @mkrieger1 Thanks for pointing me in the right direction. The solution is getattr(x, column_name). Your first recommended source. – Dave Dec 13 '17 at 17:56

0 Answers0