Saya mahu menggunakan WhooshAlchemy untuk carian teks penuh, tetapi apabila saya menggunakannya, saya mendapat ralat:
Config.py saya:
import os
daripada import app basedir
CSRF_ENABLED = True
SECRET_KEY = 'sukar diteka rentetan'
SQLALCHEMY_TRACK_MODIFICATIONS = False
basedir = os.pathos.padir = os.pathos.padir WHOOSH_BASE = os.path.join(basedir, 'search.db')
__init__.py:
app = Flask(__name__)
app.config.from_pyfile('config')
app.config['SQLALCHEMY_DATABASE_URI'] = \
'sqlite:///' + path.join(basedir, 'data.sqlite')
# 'mysql://root:123456@localhost/shop'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config.from_object('config')
db.init_app(app)
bootstrap.init_app(app)
login_manager.init_app(app)
from auth import auth as auth_blueprint
from main import main as main_blueprint
models.py:Catatan kelas(db.Model):
__tablename__ = 'posts'
__searchable__ = ['title']
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String)
body = db.Column(db.String)
created = db.Column(db.DateTime, index=True, default=datetime.utcnow)
clicks = db.Column(db.Integer)
comments = db.relationship('Comment', backref='post', lazy='dynamic')
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
jika enable_search:
whooshalchemy.whoosh_index(app, Post)
Ralat sudah jelas Fungsi whoosh_index memerlukan aplikasi, tetapi anda beralih kepada fungsi create_app dan semaknya!