Dynamic fields are now validated on save

(MongoEngine/mongoengine#153) (MongoEngine/mongoengine#154)
This commit is contained in:
Ross Lawley 2012-12-19 12:32:06 +00:00
parent c528ac09d6
commit 9cc02d4dbe
5 changed files with 33 additions and 1 deletions

View File

@ -130,3 +130,4 @@ that much better:
* Jakub Kot
* Jorge Bastida
* Stefan Wójcik
* Pete Campton

View File

@ -20,6 +20,7 @@ Changes in 0.8
- Inheritance is off by default (MongoEngine/mongoengine#122)
- Remove _types and just use _cls for inheritance (MongoEngine/mongoengine#148)
- Only allow QNode instances to be passed as query objects (MongoEngine/mongoengine#199)
- Dynamic fields are now validated on save (MongoEngine/mongoengine#153) (MongoEngine/mongoengine#154)
Changes in 0.7.9
================

View File

@ -245,6 +245,9 @@ class BaseDocument(object):
# Get a list of tuples of field names and their current values
fields = [(field, self._data.get(name))
for name, field in self._fields.items()]
if self._dynamic:
fields += [(field, self._data.get(name))
for name, field in self._dynamic_fields.items()]
EmbeddedDocumentField = _import_class("EmbeddedDocumentField")
GenericEmbeddedDocumentField = _import_class("GenericEmbeddedDocumentField")

View File

@ -564,6 +564,10 @@ class DynamicField(BaseField):
return StringField().prepare_query_value(op, value)
return self.to_mongo(value)
def validate(self, value, clean=True):
if hasattr(value, "validate"):
value.validate(clean=clean)
class ListField(ComplexBaseField):
"""A list field that wraps a standard field, allowing multiple instances

View File

@ -122,6 +122,29 @@ class DynamicTest(unittest.TestCase):
self.assertEqual(1, self.Person.objects(misc__hello='world').count())
def test_complex_embedded_document_validation(self):
"""Ensure embedded dynamic documents may be validated"""
class Embedded(DynamicEmbeddedDocument):
content = URLField()
class Doc(DynamicDocument):
pass
Doc.drop_collection()
doc = Doc()
embedded_doc_1 = Embedded(content='http://mongoengine.org')
embedded_doc_1.validate()
embedded_doc_2 = Embedded(content='this is not a url')
with self.assertRaises(ValidationError):
embedded_doc_2.validate()
doc.embedded_field_1 = embedded_doc_1
doc.embedded_field_2 = embedded_doc_2
with self.assertRaises(ValidationError):
doc.validate()
def test_inheritance(self):
"""Ensure that dynamic document plays nice with inheritance"""
class Employee(self.Person):