Flask-restless用unicode字段创建模型



我得到UnicodeEncodeError,当我尝试使用curl创建unicode字段的模型实例

这是我的代码,为了简单起见,我把它放在一个文件中:

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)

如何避免这个问题?我认为我需要创建预处理器,这将正确编码数据,或者有更简单的方法来解决这个问题?

这看起来像是Flask-Restless的一个bug。我注意到你已经在那里提交了一个问题。如果您找到了解决方案,请打开pull request并将答案更新给我们。

相关内容

  • 没有找到相关文章

最新更新