sum the database values and display the result in the flask-sqlalchemy template

Asked

Viewed 173 times

1

have ma table with following information:

class Registro(db.Model):
id = db.Column(db.Integer, primary_key=True)
valores = db.Column(db.Float)

------------|
  valores   |
------------|
   1.50     |
------------|
   2.40     |
------------|
   3.55     |
------------

@app.route('/', methods=['GET', 'POST'])
va = Registro.query.all()
def index():
return render_template('index.html', va=va)

wanted to sum these values and shows the result in the template. How would be the logic to achieve this result using flask-sqlalchemy?

1 answer

1


You can do it this way

from sqlalchemy.sql import func

@app.route('/')
def index():
    va = Registro.query(func.sum(Registro.valores)).all()
    return render_template('index.html', va=va)

in the index.html file

{{va}}
  • Thanks Carlos I solved with your help

  • 1

    Glad I could contribute.

  • Carlos does not want to abuse his patience because with his help I can resolve my doubt but another one has arisen. I did the same procedure in a table that contains hours in float that total result of that sum was 14.75 which is 14:47:24. Do you know any sqlalchemy instruction that can solve this FLOAT to hour conversion in "HH:MM format" ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.