1

Ubuntu 10.04.3 LTS mongodb-1.2.2-1ubuntu1.1 django 1.3 mongoengine-0.5.2 pymongo-2.1.2

model:

class User(Document):
    email = StringField(required=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)


class Comment(EmbeddedDocument):
    content = StringField()
    name = StringField(max_length=120)

class Post(Document):
    title = StringField(max_length=120, required=True)
    author = ReferenceField(User)
    tags = ListField(StringField(max_length=30))
    comments = ListField(EmbeddedDocumentField(Comment))

class TextPost(Post):
    content = StringField()

class ImagePost(Post):
    image_path = StringField()

class LinkPost(Post):
    link_url = StringField()

trying to save a Post in which the title has the caracter "é":

 john = User(email='[email protected]', first_name='John', last_name='Doe')
    john.save()


post1 = TextPost(title='Fun with MongoEnginée', author=john)
post1.content = 'Took a look at MongoEngine today, looks pretty cool.'
post1.tags = ['mongodb', 'mongoengine']
post1.save()

the following exception is thrown:

Traceback (most recent call last):
  File "/home/raton/aptana_work/test/mongo/test1/cobertura/tests.py", line 27, in create_relato
    post1.save()
  File "/home/raton/aptana_work/test/mongo/env/lib/python2.7/site-packages/mongoengine-0.5.2-py2.7.egg/mongoengine/document.py", line 149, in save
    doc = self.to_mongo()
  File "/home/raton/aptana_work/test/mongo/env/lib/python2.7/site-packages/mongoengine-0.5.2-py2.7.egg/mongoengine/base.py", line 648, in to_mongo
    data[field.db_field] = field.to_mongo(value)
  File "/home/raton/aptana_work/test/mongo/env/lib/python2.7/site-packages/mongoengine-0.5.2-py2.7.egg/mongoengine/base.py", line 127, in to_mongo
    return self.to_python(value)
  File "/home/raton/aptana_work/test/mongo/env/lib/python2.7/site-packages/mongoengine-0.5.2-py2.7.egg/mongoengine/fields.py", line 40, in to_python
    return unicode(value)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 19: ordinal not in range(128)

Any help please??

2 Answers 2

5

Try this:

post1 = TextPost(title=u'Fun with MongoEnginée', author=john)
post1.content = 'Took a look at MongoEngine today, looks pretty cool.'
post1.tags = ['mongodb', 'mongoengine']
post1.save()

The important part is declaring your string as unicode: u'Fun with MongoEnginée'

Sign up to request clarification or add additional context in comments.

1 Comment

No worries - its a python gotcha! Please mark as answered and it'll help any others that encounter this problem.
2

Is your source file encoded in UTF-8 and declared as such? You have to put this magic comment at the top:

#!/usr/bin/env python
# -*- coding: utf8 -*- 

See http://www.python.org/dev/peps/pep-0263/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.