0

New to Flask web development: I'm trying to query a MySQL Database using SQLAlchemy as my ORM inside a Flask app.

Each time there is a GET request a score is generated from a python function and a total score (i.e. sum of the column 'points') is returned in JSON to the Template.

I get a "TypeError: 'BaseQuery' object is not callable" when using sum func like this:

all_points = [Play.query(func.sum(Play.points))]

When I try using a .get() it's deplaying something funky in the Template (i.e. object Object],[object Object]) but at least it returns something:

all_points = [Play.query.get(1)]

Not sure what is my mistake : Is the query wrong or is the way I add the object to the database incorrect?

//db model

class Play(db.Model):

__tablename__ = "scoring"

rounds = db.Column(db.Integer, primary_key=True)
points = db.Column(db.Integer)

def __init__(self, points):
    self.points = points

//Marshmallow schema:

class PlaySchema(ma.Schema):
class Meta:
    fields = ('rounds','points')

play_schema = PlaySchema()
plays_schema = PlaySchema(many=True,only=('rounds', 'points'))

//score is returned from this function:

score = dice_score(d1,d2,d3,d4,d5)

//for each GET Request entry to the db as:

db.create_all()
round_score = Play(score)
db.session.add(round_score)
db.session.commit()

//problematic query:

all_points = [Play.query(func.sum(Play.points))]

//finally the result is passed to the template with dump (marshmallow):

dump_score = plays_schema.dump(all_points)
return render_template('main.html',data_get=data_get)
Cam
  • 19
  • 1
  • 7

1 Answers1

3

Play.query is not callable, use db.session.query(...) instead:

db.session.query(func.sum(Play.points))
Tiny.D
  • 6,466
  • 2
  • 15
  • 20