You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask项目中出现TypeError: 'BaseQuery' object is not callable错误的求助

Fixing the "TypeError: 'BaseQuery' object is not callable" in your Flask Form

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 18:47:37