Search code examples
pythonunicodeflasksqlalchemyflask-restless

Flask-restless create model with unicode field


I've get UnicodeEncodeError, when I try to create model instance with unicode field using curl

Here's my code, I've put it in one file for simplicity:

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)

When I try to create model, using curl:

  curl -i -H "Content-Type: application/json;charset=utf-8" -X POST -d '{"name": "ащьф"}' http://127.0.0.1:5000/api/tags

I get following error:

  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)

How can I avoid this issue? I think I need to create preprocessors, which will be encode data correctly, or is there more easier way to fix this problem?


Solution

  • This looks like a bug with Flask-Restless. I noticed you've already submitted an issue there. If you find the solution, open a pull request and update us on the answer.