[英]“Query has no attribute 'whoosh_search” AttributeError in Flask + Whoosh + SQLAlchemy
[英]AttributeError: 'BaseQuery' object has no attribute 'whoosh_search'
我正在使用Flask
( python
),創建whooshe
搜索,我已按照此鏈接中的所有步驟進行操作,也從其他鏈接中嘗試過,但每次最后我都會遇到此錯誤:
results = BlogPost.query.whoosh_search('cool')
AttributeError:'BaseQuery' object has no attribute 'whoosh_search'
這是我的模型代碼:
class BlogPost(db.Model):
__tablename__ = 'blogpost'
__searchable__ = ['title', 'content']
__analyzer__ = StemmingAnalyzer()
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode) # Indexed fields are either String,
content = db.Column(db.Text) # Unicode, or Text
created = db.Column(db.DateTime, default=datetime.utcnow)
我在這方面有錯誤:
@app.route('/search')
def search():
results = BlogPost.query.whoosh_search('cool')
return results
我也看到了這個錯誤。然后我發現錯誤在models.py中。您應該在模型代碼的末尾添加whooshalchemy.whoosh_index(app, BlogPost)
。
如果有人掉下這個兔子洞,我會遇到類似的問題。
就我而言,我發現在我的models.py
我從 sqlalchemy 導入並使用 declarative_base 作為每個模型的基礎。 IE:
from sqlalchemy import Column, Integer, DateTime, Text, Boolean
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Book(Base):
在我的routes.py
我導入了flask-sqlalchemy
,您使用 db.Model 作為基礎:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db.init_app(app)
class Book(db.Model):
在其他一些帖子上也有一些類似的答案,但看起來在使用 flask-whooshalchemy 或其類似變體(whooshalchemy3,whooshalchemyplus)時,使用 db.Model 作為基礎是初始化模型以具有適當屬性的正確方法.
希望這可以幫助某人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.