Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when creating unicode model #344

Closed
citizen-stig opened this issue Aug 8, 2014 · 6 comments
Closed

Error when creating unicode model #344

citizen-stig opened this issue Aug 8, 2014 · 6 comments

Comments

@citizen-stig
Copy link
Contributor

@citizen-stig citizen-stig commented Aug 8, 2014

I have a simple SQLAlchemy model with one UnicodeText column.

When I try to create new item using curl I get following exception.

    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-6: ordinal not in range(128)

By the way, database entry successfully appears. I use python 2

@citizen-stig
Copy link
Contributor Author

@citizen-stig citizen-stig commented Aug 8, 2014

I think quote_plus() should be applied somewhere in this code

Loading

@jfinkels
Copy link
Owner

@jfinkels jfinkels commented Aug 8, 2014

Please provide the code (or at the very least, the unicode string) that caused this error.

By the way, the result appears in the database because the session is committed immediately before this line.

Loading

@citizen-stig
Copy link
Contributor Author

@citizen-stig citizen-stig commented Aug 8, 2014

Actually any non-ascii unicode string causes this error. For example:

test_str = u'ЮникодТест'

I've created stackoverflow question for this: http://stackoverflow.com/questions/25205585/flask-restless-create-model-with-unicode-field

Loading

@citizen-stig
Copy link
Contributor Author

@citizen-stig citizen-stig commented Aug 8, 2014

By the way, I've made small fix on my machine.

prekey = result[primary_key_name(instance)]
if type(prekey) == unicode:
    primary_key = quote_plus(prekey.encode('utf-8'))
else:
    primary_key = prekey

Currently I'm trying to create a fix in my fork.

Loading

@citizen-stig
Copy link
Contributor Author

@citizen-stig citizen-stig commented Aug 8, 2014

I've created pull request #345

Loading

@jfinkels
Copy link
Owner

@jfinkels jfinkels commented Aug 12, 2014

I merged that pull request, thanks.

Loading

@jfinkels jfinkels closed this Aug 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants