diff --git a/docs/changelog.rst b/docs/changelog.rst index f3e83c1b..b2a855d5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,7 @@ Changelog Changes in 0.7.X ================ +- Unicode fix for repr (MongoEngine/mongoengine#133) - Allow updates with match operators (MongoEngine/mongoengine#144) - Updated URLField - now can have a override the regex (MongoEngine/mongoengine#136) - Allow Django AuthenticationBackends to work with Django user (hmarr/mongoengine#573) diff --git a/mongoengine/base.py b/mongoengine/base.py index 342b31ec..714b74ba 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -1333,10 +1333,11 @@ class BaseDocument(object): def __repr__(self): try: - u = txt_type(self) + u = self.__str__() except (UnicodeEncodeError, UnicodeDecodeError): u = '[Bad Unicode data]' - return '<%s: %s>' % (self.__class__.__name__, u) + repr_type = type(u) + return repr_type('<%s: %s>' % (self.__class__.__name__, u)) def __str__(self): if hasattr(self, '__unicode__'): @@ -1344,7 +1345,7 @@ class BaseDocument(object): return self.__unicode__() else: return unicode(self).encode('utf-8') - return '%s object' % self.__class__.__name__ + return txt_type('%s object' % self.__class__.__name__) def __eq__(self, other): if isinstance(other, self.__class__) and hasattr(other, 'id'): diff --git a/tests/test_document.py b/tests/test_document.py index d2969fe6..60a966b7 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -85,6 +85,22 @@ class DocumentTest(unittest.TestCase): # Ensure Document isn't treated like an actual document self.assertFalse(hasattr(Document, '_fields')) + def test_repr(self): + """Ensure that unicode representation works + """ + class Article(Document): + title = StringField() + + def __unicode__(self): + return self.title + + Article.drop_collection() + + Article(title=u'привет мир').save() + + self.assertEqual('', repr(Article.objects.first())) + self.assertEqual('[]', repr(Article.objects.all())) + def test_collection_naming(self): """Ensure that a collection with a specified name may be used. """