handle None from model __str__; Fixes #753

This commit is contained in:
Vjacheslav Murashkin 2014-09-04 16:54:23 +04:00
parent 759f72169a
commit 7430b31697
3 changed files with 15 additions and 1 deletions

View File

@ -210,3 +210,4 @@ that much better:
* Jay Shirley (https://github.com/jshirley) * Jay Shirley (https://github.com/jshirley)
* DavidBord (https://github.com/DavidBord) * DavidBord (https://github.com/DavidBord)
* Axel Haustant (https://github.com/noirbizarre) * Axel Haustant (https://github.com/noirbizarre)
* Vyacheslav Murashkin (https://github.com/a4tunado)

View File

@ -229,7 +229,7 @@ class BaseDocument(object):
u = self.__str__() u = self.__str__()
except (UnicodeEncodeError, UnicodeDecodeError): except (UnicodeEncodeError, UnicodeDecodeError):
u = '[Bad Unicode data]' u = '[Bad Unicode data]'
repr_type = type(u) repr_type = str if u is None else type(u)
return repr_type('<%s: %s>' % (self.__class__.__name__, u)) return repr_type('<%s: %s>' % (self.__class__.__name__, u))
def __str__(self): def __str__(self):

View File

@ -103,6 +103,19 @@ class InstanceTest(unittest.TestCase):
self.assertEqual('<Article: привет мир>', repr(doc)) self.assertEqual('<Article: привет мир>', repr(doc))
def test_repr_none(self):
"""Ensure None values handled correctly
"""
class Article(Document):
title = StringField()
def __str__(self):
return None
doc = Article(title=u'привет мир')
self.assertEqual('<Article: None>', repr(doc))
def test_queryset_resurrects_dropped_collection(self): def test_queryset_resurrects_dropped_collection(self):
self.Person.drop_collection() self.Person.drop_collection()