Accessing a missing field now returns None rather than raising an AttributeError

This commit is contained in:
Harry Marr 2009-12-24 17:10:36 +00:00
parent 3d70b65a45
commit f687bad202
5 changed files with 14 additions and 17 deletions

View File

@ -213,8 +213,6 @@ saved::
>>> page = Page(title="Test Page") >>> page = Page(title="Test Page")
>>> page.id >>> page.id
...
AttributeError('_id')
>>> page.save() >>> page.save()
>>> page.id >>> page.id
ObjectId('123456789abcdef000000000') ObjectId('123456789abcdef000000000')

View File

@ -28,12 +28,10 @@ class BaseField(object):
# Get value from document instance if available, if not use default # Get value from document instance if available, if not use default
value = instance._data.get(self.name) value = instance._data.get(self.name)
if value is None: if value is None:
if self.default is not None: value = self.default
value = self.default # Allow callable default values
if callable(value): if callable(value):
value = value() value = value()
else:
raise AttributeError(self.name)
return value return value
def __set__(self, instance, value): def __set__(self, instance, value):
@ -227,8 +225,8 @@ class BaseDocument(object):
def __contains__(self, name): def __contains__(self, name):
try: try:
getattr(self, name) val = getattr(self, name)
return True return val is not None
except AttributeError: except AttributeError:
return False return False

View File

@ -181,9 +181,8 @@ class ReferenceField(BaseField):
if isinstance(document, (str, unicode, pymongo.objectid.ObjectId)): if isinstance(document, (str, unicode, pymongo.objectid.ObjectId)):
id_ = document id_ = document
else: else:
try: id_ = document.id
id_ = document.id if id_ is None:
except:
raise ValidationError('You can only reference documents once ' raise ValidationError('You can only reference documents once '
'they have been saved to the database') 'they have been saved to the database')

View File

@ -228,7 +228,7 @@ class DocumentTest(unittest.TestCase):
person_obj = collection.find_one({'name': 'Test User'}) person_obj = collection.find_one({'name': 'Test User'})
self.assertEqual(person_obj['name'], 'Test User') self.assertEqual(person_obj['name'], 'Test User')
self.assertEqual(person_obj['age'], 30) self.assertEqual(person_obj['age'], 30)
self.assertEqual(person_obj['_id'], person.id) self.assertEqual(str(person_obj['_id']), person.id)
def test_delete(self): def test_delete(self):
"""Ensure that document may be deleted using the delete method. """Ensure that document may be deleted using the delete method.

View File

@ -46,7 +46,7 @@ class FieldTest(unittest.TestCase):
name = StringField() name = StringField()
person = Person(name='Test User') person = Person(name='Test User')
self.assertRaises(AttributeError, getattr, person, 'id') self.assertEqual(person.id, None)
self.assertRaises(ValidationError, person.__setattr__, 'id', 47) self.assertRaises(ValidationError, person.__setattr__, 'id', 47)
self.assertRaises(ValidationError, person.__setattr__, 'id', 'abc') self.assertRaises(ValidationError, person.__setattr__, 'id', 'abc')
person.id = '497ce96f395f2f052a494fd4' person.id = '497ce96f395f2f052a494fd4'
@ -173,8 +173,8 @@ class FieldTest(unittest.TestCase):
post.author = PowerUser(name='Test User', power=47) post.author = PowerUser(name='Test User', power=47)
def test_reference_validation(self): def test_reference_validation(self):
"""Ensure that invalid embedded documents cannot be assigned to """Ensure that invalid docment objects cannot be assigned to reference
embedded document fields. fields.
""" """
class User(Document): class User(Document):
name = StringField() name = StringField()
@ -187,10 +187,12 @@ class FieldTest(unittest.TestCase):
user = User(name='Test User') user = User(name='Test User')
# Ensure that the referenced object must have been saved
post1 = BlogPost(content='Chips and gravy taste good.') post1 = BlogPost(content='Chips and gravy taste good.')
post1.author = user post1.author = user
self.assertRaises(ValidationError, post1.save) self.assertRaises(ValidationError, post1.save)
# Check that an invalid object type cannot be used
post2 = BlogPost(content='Chips and chilli taste good.') post2 = BlogPost(content='Chips and chilli taste good.')
self.assertRaises(ValidationError, post1.__setattr__, 'author', post2) self.assertRaises(ValidationError, post1.__setattr__, 'author', post2)