Flask-WhooshAlchemy引发NameError:名称“ unicode”未定义

时间:2018-08-07 14:01:13

标签: python flask whoosh

尝试使用WhooshAlchemy在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)
Traceback (most recent call last):
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask_login/utils.py", line 261, in decorated_view
    return func(*args, **kwargs)
  File "/Users/vladimir/PycharmProjects/adminFlask/auth/routes.py", line 77, in results
    result = Employee.query.whoosh_search('q').all()
  File "/Users/vladimir/PycharmProjects/adminFlask/venv/lib/python3.6/site-packages/flask_whooshalchemy.py", line 103, in whoosh_search
    if not isinstance(query, unicode):
NameError: name 'unicode' is not defined

2 个答案:

答案 0 :(得分:0)

好像Flask-WhooshAlchemy尚未准备好用于Python3,如果我们查看code on github,就会发现以下相关部分:

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

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

现在,这是指unicode内置类型,在Python3中不再可用,请在解释器Python2中查看此练习:

Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode
<type 'unicode'>

在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

答案 1 :(得分:0)

我刚刚也解决了这个问题,显然它与python 3不兼容。我使用的解决方案是here

  1. 卸载你的 whoosh alchemy 版本:

    $ flask/bin/pip uninstall flask-whooshalchemy
    
  2. 从 Miguel Grinberg 安装此版本:

    flask/bin/pip install git+git://github.com/miguelgrinberg/flask-whooshalchemy.git
    
  3. 删除你的数据库并重新创建它......它应该可以工作