[FIXED] error – "TypeError: 'BaseQuery' object is not callable"

Issue

from flask_restful import Api , Resource
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms_sqlalchemy.fields import QuerySelectField
app = Flask (__name__, template_folder = 'template')


app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///MotorsportCalendarTest.db'
app.config['SECRET_KEY'] = 'secret'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class Choice(db.Model):
    Series = db.Column(db.String , primary_key = True)
    Circuit = db.Column(db.String, primary_key = True)
    Day = db.Column(db.Integer, primary_key = True)
    Month = db.Column(db.String, primary_key = True)
def choice_query():
    return Choice.query

class ChoiceForm(FlaskForm):
    Series = QuerySelectField(query_factory= choice_query(), allow_blank = True)

@app.route('/')
def index():
    form =  ChoiceForm()
    return render_template('index.html', form = form)
    

if __name__ == "__main__":
    app.run(debug = True)

I haven’t called Basequery at any point so I’m not sure why I’m getting this error, I’ve had a look online but still can’t make sense of the problem so any help would be greatly appricated.

Solution

If I understand the problem correctly, the error is that you are calling the callback function for the query_factory attribute inside the constructor instead of passing it.

The code should be as follows.

def choice_query():
    return Choice.query

class ChoiceForm(FlaskForm):
    Series = QuerySelectField(query_factory=choice_query, allow_blank=True)

Answered By – Detlef

Answer Checked By – Candace Johnson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published