Trying to create a search in a Flask app with WhooshAlchemy, but got an error.
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
Looks like Flask-WhooshAlchemy is not ready for Python3, if we look at the code on github, we find this relevant part:
if not isinstance(query, unicode):
query = unicode(query)
results = self._whoosh_searcher(query, limit, fields, or_)
Now, this refers to the unicode
built-in type, which is no longer available in Python3, see this exercise in the interpreter, 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'>
And doing the same in Python3, we find we get the error you are experiencing:
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