【发布时间】:2019-06-01 17:22:09
【问题描述】:
这是我的models.py
class Categories(db.Model):
__tablename__ ='categories'
parent = db.Column(db.String, nullable = False)
child = db.Column(db.String)
pid = db.Column(db.Integer, primary_key = True)
def __repr__(self):
return f"Categories('{self.parent}', '{self.child}', '{self.pid}')"
def __init__(self, parent, child, id):
self.parent = parent
self.child = child
self.id = id
这是我的forms.py
existSelectParentField = QuerySelectField('Select Existing Categories',query_factory=parentcat_query, get_label= 'parent', description='Choose parent category')
这是我的查询工厂
def parentcat_query():
parentcat = session.query(db.func.count(Categories.child),
Categories.parent).group_by(Categories.parent).all()
return parentcat
但我收到此错误,sqlalchemy.orm.exc.UnmappedInstanceError: Class 'sqlalchemy.util._collections.result' is not mapped
我想做的是从我的 Categories.parent 中显示不同的值
我知道我必须返回一个带有查询工厂的表,而不仅仅是一列,但在这种情况下,我要返回一个包含两列的表。
是因为这个“新表”没有映射到我的models.py中吗?
对解决方法有什么建议吗?
【问题讨论】:
-
无法在 QuerySelectfield 中从模型中选择细节,只能进行额外过滤(filter 或 filter_by)。看看这个stackoverflow.com/questions/53885973/… 我是如何解决它的。基本上,我使用特定代码创建了一个自定义 QuerySelectField 来删除重复项。
标签: python flask sqlalchemy flask-sqlalchemy flask-wtforms