From da57572409b9b7819c886da581ec07859d6805ae Mon Sep 17 00:00:00 2001 From: Florian Schlachter Date: Sat, 17 Apr 2010 01:23:14 +0200 Subject: [PATCH] Introduced new create_default field argument. If set to true, mongoengine will automagically create an instance of the desired document class (useful if using EmbeddedDocumentField for example): class SubDoc(EmbeddedDocument): url = URLField() class MyDoc(Document): subdoc = EmbeddedDocumentField(SubDoc, create_default=True) With create_default MyDoc().subdoc is automatically instantiated. Hint: default=SubDoc() WON'T work (that's why I've introduced create_default) --- mongoengine/base.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mongoengine/base.py b/mongoengine/base.py index 24949490..c5f0b554 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -24,7 +24,8 @@ class BaseField(object): _index_with_types = True def __init__(self, db_field=None, name=None, required=False, default=None, - unique=False, unique_with=None, primary_key=False, validation=None): + unique=False, unique_with=None, primary_key=False, validation=None, + create_default=False): self.db_field = (db_field or name) if not primary_key else '_id' if name: import warnings @@ -37,6 +38,7 @@ class BaseField(object): self.unique_with = unique_with self.primary_key = primary_key self.validation = validation + self.create_default = create_default def __get__(self, instance, owner): """Descriptor for retrieving a value from a field in a document. Do @@ -53,6 +55,10 @@ class BaseField(object): # Allow callable default values if callable(value): value = value() + + # Check whether we should auto-create a default document + if self.create_default and hasattr(self, 'document'): + value = self.document() return value def __set__(self, instance, value):