我正在尝试使用flask-sqlalchemy从下表中检索数据,使用的数据库是MS SQL 2008 r2。我遇到的问题是标题字段中的数据,因为它混合了用户输入的标准和非英文字符。
class MovieCountry(db.Model):
__tablename__ = 'movie_country'
movie_id = db.Column('movie_id', db.Integer, primary_key=True)
country_id = db.Column('country_id', db.Integer, primary_key=True)
title= db.Column('movie_title', db.VARCHAR(length=50,
convert_unicode='force', unicode_error='replace', _warn_on_bytestring=False))
我在sqlalchemy doc中建议添加了unicode转换选项,只要我打印出结果并且不尝试将它们返回到我正在处理的网站上的其他地方使用,否则我得到以下内容错误 AttributeError:'list'对象没有属性'encode'。
我正在使用的查询是:
def get_movie_titles_by_country(country_id):
result = MovieCountry.query.with_entities(MovieCountry.movie_id, MovieCountry.title)\
.filter(MovieCountry.country_id == country_id)\
.order_by(MovieCountry.title).all()
return result
我假设错误是由于 .all()将数据作为列表检索,但是我如何从此表中正确检索?
view.py
@app.route('/index/_movies_list', methods=['POST', 'GET'])
def movies():
country_code = request.args.get('country_code', 0, type=str)
movie_list = queries._get_movie_list_by_country(country_code)
return movie_list
下面是追溯
File "C:\Python27\Lib\site-packages\flask-0.9-py2.7.egg\flask\app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\Lib\site-packages\flask-0.9-py2.7.egg\flask\app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\Lib\site-packages\flask-0.9-py2.7.egg\flask\app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\Lib\site-packages\flask-0.9-py2.7.egg\flask\app.py", line 1361, in full_dispatch_request
response = self.make_response(rv)
File "C:\Python27\Lib\site-packages\flask-0.9-py2.7.egg\flask\app.py", line 1447, in make_response
rv = self.response_class(rv, headers=headers, status=status)
File "J:\Programming\Python\Web Development\Flask\WebToolsV2\venv\lib\site-packages\werkzeug\wrappers.py", line 758, in __init__
self.status = status
File "J:\Programming\Python\Web Development\Flask\WebToolsV2\venv\lib\site-packages\werkzeug\wrappers.py", line 862, in _set_status
self._status = to_native(value)
File "J:\Programming\Python\Web Development\Flask\WebToolsV2\venv\lib\site-packages\werkzeug\_compat.py", line 111, in to_native
return x.encode(charset, errors)
AttributeError: 'list' object has no attribute 'encode'