Flask-restless 创建带有 unicode 字段的模型
Posted
技术标签:
【中文标题】Flask-restless 创建带有 unicode 字段的模型【英文标题】:Flask-restless create model with unicode field 【发布时间】:2014-10-02 01:21:31 【问题描述】:当我尝试使用 curl 使用 unicode 字段创建模型实例时,我遇到了 UnicodeEncodeError
这是我的代码,为简单起见,我将其放在一个文件中:
import flask
import flask.ext.sqlalchemy
import flask.ext.restless
from flask import send_from_directory
from sqlalchemy import Column, UnicodeText
from sqlalchemy.ext.declarative import declarative_base
app = flask.Flask(__name__, static_url_path='')
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://scott:tiger@localhost:5432/db'
db = flask.ext.sqlalchemy.SQLAlchemy(app)
Base = declarative_base()
class Tag(Base):
__tablename__ = 'tags'
name = Column(UnicodeText, primary_key=True)
# debug only
@app.route('/')
def index():
return send_from_directory('templates', 'index.html')
if __name__ == '__main__':
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(Tag, methods=['GET', 'POST', 'DELETE'])
# start the flask loop
app.run(host='0.0.0.0', debug=True)
当我尝试使用 curl 创建模型时:
curl -i -H "Content-Type: application/json;charset=utf-8" -X POST -d '"name": "ащьф"' http://127.0.0.1:5000/api/tags
我收到以下错误:
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/flask_restless/views.py", line 139, in decorator
return func(*args, **kw)
File "/usr/local/lib/python2.7/dist-packages/mimerender.py", line 227, in wrapper
result = target(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/views.py", line 149, in dispatch_request
return meth(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask_restless/views.py", line 1230, in post
primary_key = str(result[primary_key_name(instance)])
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)
如何避免这个问题?我想我需要创建预处理器来正确编码数据,还是有更简单的方法来解决这个问题?
【问题讨论】:
【参考方案1】:这看起来像是 Flask-Restless 的错误。我注意到您已经在那里提交了issue。如果您找到了解决方案,请打开拉取请求并更新我们的答案。
【讨论】:
是的,它看起来像一个错误。我创建了拉取请求github.com/jfinkels/flask-restless/pull/345。但也许没有修改flask-restless代码的解决方法? 您的拉取请求看起来不错,在我看来是有效的解决方案。解决方法可能是在发送之前以某种方式对ащьф
进行编码,但您的拉取请求更清晰
拉取请求已合并以上是关于Flask-restless 创建带有 unicode 字段的模型的主要内容,如果未能解决你的问题,请参考以下文章
带有mod_wsgi的flask-restless无法连接到MySQL服务器
flask-restless 限制 RESTful api 访问