flaskwhooshelchemy引发名称错误:未定义名称“unicode”

2024-06-28 12:01:02 发布

您现在位置:Python中文网/ 问答频道 /正文

试图用whooshemmy在Flask应用程序中创建搜索,但出现错误。

class Employee(db.Model):
    __searchable__ = ['first_name', 'patronymic', 'last_name', 
    'position', 'office', 'birthday']

    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(60))
    patronymic = db.Column(db.String(60))
    last_name = db.Column(db.String(60))
    position = db.Column(db.String(60))
    office = db.Column(db.String)
    birthday = db.Column(db.Date)

    def __init__(self, first_name, patronymic, last_name, position, 
    office, birthday):
        self.first_name = first_name
        self.patronymic = patronymic
        self.last_name = last_name
        self.position = position
        self.office = office
        self.birthday = birthday

    def __repr__(self):
        return '<First name: {}, Patronymic: {}, Last name: {}, 
    Position: {}, Office: {}, Birthday: {}>'\
            .format(self.first_name, self.patronymic, self.last_name, 
    self.position, self.office, self.birthday)


wa.whoosh_index(app, Employee)


@app.route('/results')
@login_required
def results():
    q = request.args.get('q')
    if q:
        result = Employee.query.whoosh_search('q').all()
    else:
        result = Employee.query.all()

    return render_template('results.html', result=result)
^{pr2}$

Tags: nameselfdbstringdefemployeepositioncolumn
1条回答
网友
1楼 · 发布于 2024-06-28 12:01:02

看起来烧瓶呼呼炼金术还没准备好,如果我们看一下code on github,我们会找到相关的部分:

    if not isinstance(query, unicode):
        query = unicode(query)

    results = self._whoosh_searcher(query, limit, fields, or_)

现在,这指的是unicode内置类型,它在Python3中不再可用,请参见解释器Python2中的这个练习:

^{pr2}$

在Python3中执行同样的操作,我们会发现您遇到的错误:

Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'unicode' is not defined

相关问题 更多 >