From 40e99b1b80479e3dda631ab551a318d21d50da0a Mon Sep 17 00:00:00 2001 From: anih Date: Wed, 27 Jul 2016 12:10:46 +0200 Subject: [PATCH 1/2] Speed up checking if we passed missing field --- mongoengine/base/document.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index 448f8389..fadda159 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -72,12 +72,13 @@ class BaseDocument(object): # Check if there are undefined fields supplied to the constructor, # if so raise an Exception. if not self._dynamic and (self._meta.get('strict', True) or _created): - for var in values.keys(): - if var not in self._fields.keys() + ['id', 'pk', '_cls', '_text_score']: - msg = ( - "The field '{0}' does not exist on the document '{1}'" - ).format(var, self._class_name) - raise FieldDoesNotExist(msg) + _missing_fields_keys = set(values.keys()) - set( + self._fields.keys() + ['id', 'pk', '_cls', '_text_score']) + if _missing_fields_keys: + msg = ( + "The fields '{0}' does not exist on the document '{1}'" + ).format(_missing_fields_keys, self._class_name) + raise FieldDoesNotExist(msg) if self.STRICT and not self._dynamic: self._data = StrictDict.create(allowed_keys=self._fields_ordered)() From bd84667a2b6db42d3ac59dfa5baed047374e338b Mon Sep 17 00:00:00 2001 From: anih Date: Tue, 6 Sep 2016 09:27:41 +0200 Subject: [PATCH 2/2] fixes --- mongoengine/base/document.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index fadda159..68a00485 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -72,12 +72,12 @@ class BaseDocument(object): # Check if there are undefined fields supplied to the constructor, # if so raise an Exception. if not self._dynamic and (self._meta.get('strict', True) or _created): - _missing_fields_keys = set(values.keys()) - set( + _undefined_fields = set(values.keys()) - set( self._fields.keys() + ['id', 'pk', '_cls', '_text_score']) - if _missing_fields_keys: + if _undefined_fields: msg = ( - "The fields '{0}' does not exist on the document '{1}'" - ).format(_missing_fields_keys, self._class_name) + "The fields '{0}' do not exist on the document '{1}'" + ).format(_undefined_fields, self._class_name) raise FieldDoesNotExist(msg) if self.STRICT and not self._dynamic: