Skip to content

Commit e66c492

Browse files
author
Jan Schrewe
committed
Test for parent_embedded_field before using it
1 parent 26d1d69 commit e66c492

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

mongodbforms/documents.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ def documentform_factory(document, form=DocumentForm, fields=None, exclude=None,
477477
class EmbeddedDocumentForm(with_metaclass(DocumentFormMetaclass, BaseDocumentForm)):
478478

479479
def __init__(self, parent_document, instance=None, position=None, *args, **kwargs):
480+
if self._meta.embedded_field is not None and not \
481+
self._meta.embedded_field in parent_document._fields:
482+
raise FieldError("Parent document must have field %s" % self._meta.embedded_field)
483+
480484
# if we received a list position of the instance and no instance
481485
# load the instance from the parent document and preceed as normal
482486
if instance is None and position is not None:
@@ -485,9 +489,6 @@ def __init__(self, parent_document, instance=None, position=None, *args, **kwarg
485489
super(EmbeddedDocumentForm, self).__init__(instance=instance, *args, **kwargs)
486490
self.parent_document = parent_document
487491
self.position = position
488-
if self._meta.embedded_field is not None and not \
489-
self._meta.embedded_field in self.parent_document._fields:
490-
raise FieldError("Parent document must have field %s" % self._meta.embedded_field)
491492

492493
def save(self, commit=True):
493494
"""If commit is True the embedded document is added to the parent

0 commit comments

Comments
 (0)