我正在使用monkey_patch()
(Flask
),创建python
搜索,我已按照此link的所有步骤操作,也尝试过其他链接,但每次都是最后我遇到了这个错误:
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)
答案 0 :(得分:1)
我也看到了这个错误。然后我发现错误在models.py中。你应该在模型代码的末尾添加whooshalchemy.whoosh_index(app, BlogPost)
。
答案 1 :(得分:0)
如果有人掉进这个兔子洞,我会遇到类似的问题。
就我而言,我发现在我的 models.py
中,我从 sqlalchemy 导入并使用 declarative_base 作为每个模型的基础。
即:
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 作为基础是初始化模型的正确方法适当的属性。
希望能帮助到后来的人。