diff --git a/docs/changelog.rst b/docs/changelog.rst index 830c0192..a4a7fa9e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,7 +5,11 @@ Changelog Changes in dev ============== +<<<<<<< HEAD - Added spec file for rpm-based distributions +======= +- Updated ComplexFields so if required they won't accept empty lists / dicts +>>>>>>> listfield - Fixed ListField so it doesnt accept strings - Added DynamicDocument and EmbeddedDynamicDocument classes for expando schemas diff --git a/mongoengine/base.py b/mongoengine/base.py index 40953f17..adf5eee9 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -282,6 +282,11 @@ class ComplexBaseField(BaseField): raise ValidationError('Invalid %s item (%s)' % ( self.field.__class__.__name__, str(v))) + # Don't allow empty values if required + if self.required and not value: + raise ValidationError('Field "%s" is required and cannot be empty' % + self.name) + def prepare_query_value(self, op, value): return self.to_mongo(value) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 705bf3ae..e75890f7 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -459,6 +459,9 @@ class GenericEmbeddedDocumentField(BaseField): class ListField(ComplexBaseField): """A list field that wraps a standard field, allowing multiple instances of the field to be used as a list in the database. + + .. note:: + Required means it cannot be empty - as the default for ListFields is [] """ # ListFields cannot be indexed with _types - MongoDB doesn't support this @@ -513,6 +516,9 @@ class DictField(ComplexBaseField): """A dictionary field that wraps a standard Python dictionary. This is similar to an embedded document, but the structure is not defined. + .. note:: + Required means it cannot be empty - as the default for ListFields is [] + .. versionadded:: 0.3 .. versionchanged:: 0.5 - Can now handle complex / varying types of data """ diff --git a/tests/fields.py b/tests/fields.py index fd993167..b1825026 100644 --- a/tests/fields.py +++ b/tests/fields.py @@ -485,7 +485,6 @@ class FieldTest(unittest.TestCase): post.info = [{'test': 3}] post.save() - self.assertEquals(BlogPost.objects.count(), 3) self.assertEquals(BlogPost.objects.filter(info__exact='test').count(), 1) self.assertEquals(BlogPost.objects.filter(info__0__test='test').count(), 1) @@ -515,7 +514,6 @@ class FieldTest(unittest.TestCase): Simple.drop_collection() - def test_list_field_rejects_strings(self): """Strings aren't valid list field data types""" @@ -528,6 +526,26 @@ class FieldTest(unittest.TestCase): self.assertRaises(ValidationError, e.save) + def test_complex_field_required(self): + """Ensure required cant be None / Empty""" + + class Simple(Document): + mapping = ListField(required=True) + + Simple.drop_collection() + e = Simple() + e.mapping = [] + + self.assertRaises(ValidationError, e.save) + + class Simple(Document): + mapping = DictField(required=True) + + Simple.drop_collection() + e = Simple() + e.mapping = {} + + self.assertRaises(ValidationError, e.save) def test_list_field_complex(self): """Ensure that the list fields can handle the complex types."""