Flask项目中出现TypeError: 'BaseQuery' object is not callable错误的求助
Hey there, I see exactly what's causing this error in your code! Let's break it down:
The issue lies in how you're setting up the query_factory parameter for your QuerySelectField in the ChoiceForm class. Right now you have:
Series = QuerySelectField(query_factory= choice_query(), allow_blank = True)
When you add parentheses () to choice_query(), you're immediately running the function and passing its return value (a BaseQuery object from Choice.query) to query_factory. But here's the thing: QuerySelectField expects query_factory to be a function reference—not the result of calling that function. It will call the function on its own when the form initializes. Since you're passing a BaseQuery instead of a function, it tries to "call" that BaseQuery object later, which triggers the TypeError you're seeing.
The Simple Fix
Just remove the parentheses from choice_query() so you pass the function itself, not its executed result:
class ChoiceForm(FlaskForm): Series = QuerySelectField(query_factory=choice_query, allow_blank=True)
Why This Works
Now query_factory holds the choice_query function object. When the form is created, it will call choice_query() internally, which returns the correct BaseQuery to populate your select field options.
One quick extra check: I noticed your code is missing the import for render_template—make sure to add that at the top of your file:
from flask import Flask, render_template
That should resolve the error and get your form up and running smoothly!
内容的提问来源于stack exchange,提问作者Oliver Shotton

