Updates to ComplexFields
Required now means they cannot be empty [#302]
This commit is contained in:
parent
17728d4e74
commit
6961a9494f
@ -5,6 +5,7 @@ Changelog
|
|||||||
Changes in dev
|
Changes in dev
|
||||||
==============
|
==============
|
||||||
|
|
||||||
|
- Updated ComplexFields so if required they won't accept empty lists / dicts
|
||||||
- Fixed ListField so it doesnt accept strings
|
- Fixed ListField so it doesnt accept strings
|
||||||
- Added DynamicDocument and EmbeddedDynamicDocument classes for expando schemas
|
- Added DynamicDocument and EmbeddedDynamicDocument classes for expando schemas
|
||||||
|
|
||||||
|
@ -282,6 +282,11 @@ class ComplexBaseField(BaseField):
|
|||||||
raise ValidationError('Invalid %s item (%s)' % (
|
raise ValidationError('Invalid %s item (%s)' % (
|
||||||
self.field.__class__.__name__, str(v)))
|
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):
|
def prepare_query_value(self, op, value):
|
||||||
return self.to_mongo(value)
|
return self.to_mongo(value)
|
||||||
|
|
||||||
|
@ -459,6 +459,9 @@ class GenericEmbeddedDocumentField(BaseField):
|
|||||||
class ListField(ComplexBaseField):
|
class ListField(ComplexBaseField):
|
||||||
"""A list field that wraps a standard field, allowing multiple instances
|
"""A list field that wraps a standard field, allowing multiple instances
|
||||||
of the field to be used as a list in the database.
|
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
|
# ListFields cannot be indexed with _types - MongoDB doesn't support this
|
||||||
@ -476,10 +479,6 @@ class ListField(ComplexBaseField):
|
|||||||
isinstance(value, basestring)):
|
isinstance(value, basestring)):
|
||||||
raise ValidationError('Only lists and tuples may be used in a '
|
raise ValidationError('Only lists and tuples may be used in a '
|
||||||
'list field')
|
'list field')
|
||||||
# don't allow empty lists when they are required
|
|
||||||
if self.required and not value:
|
|
||||||
raise ValidationError('Field "%s" is required and cannot be empty' %
|
|
||||||
self.name)
|
|
||||||
super(ListField, self).validate(value)
|
super(ListField, self).validate(value)
|
||||||
|
|
||||||
def prepare_query_value(self, op, value):
|
def prepare_query_value(self, op, value):
|
||||||
@ -517,6 +516,9 @@ class DictField(ComplexBaseField):
|
|||||||
"""A dictionary field that wraps a standard Python dictionary. This is
|
"""A dictionary field that wraps a standard Python dictionary. This is
|
||||||
similar to an embedded document, but the structure is not defined.
|
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
|
.. versionadded:: 0.3
|
||||||
.. versionchanged:: 0.5 - Can now handle complex / varying types of data
|
.. versionchanged:: 0.5 - Can now handle complex / varying types of data
|
||||||
"""
|
"""
|
||||||
|
@ -526,7 +526,7 @@ class FieldTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertRaises(ValidationError, e.save)
|
self.assertRaises(ValidationError, e.save)
|
||||||
|
|
||||||
def test_list_field_required(self):
|
def test_complex_field_required(self):
|
||||||
"""Ensure required cant be None / Empty"""
|
"""Ensure required cant be None / Empty"""
|
||||||
|
|
||||||
class Simple(Document):
|
class Simple(Document):
|
||||||
@ -538,6 +538,15 @@ class FieldTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertRaises(ValidationError, e.save)
|
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):
|
def test_list_field_complex(self):
|
||||||
"""Ensure that the list fields can handle the complex types."""
|
"""Ensure that the list fields can handle the complex types."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user