This is my 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
This is my forms.py
existSelectParentField = QuerySelectField('Select Existing Categories',query_factory=parentcat_query, get_label= 'parent', description='Choose parent category')
This is my query factory
def parentcat_query():
parentcat = session.query(db.func.count(Categories.child),
Categories.parent).group_by(Categories.parent).all()
return parentcat
But I get this error, sqlalchemy.orm.exc.UnmappedInstanceError: Class 'sqlalchemy.util._collections.result' is not mapped
What I'd like to do is display distinct values from my Categories.parent
I understand that I must return a table with a query factory and not just a column but in this case I am returning a table with two columns.
Is the error due to the fact that this 'new table' has not been mapped in my models.py?
Any suggestions on a workaround?