Added note about the use of default parameters

This commit is contained in:
Jaime 2010-10-06 19:56:12 +01:00
parent 196606438c
commit 833fa3d94d

View File

@ -66,6 +66,25 @@ arguments can be set on all fields:
:attr:`default` (Default: None) :attr:`default` (Default: None)
A value to use when no value is set for this field. A value to use when no value is set for this field.
The definion of default parameters follow `the general rules on Python
<http://docs.python.org/reference/compound_stmts.html#function-definitions>`__,
which means that some care should be taken when dealing with default mutable objects
(like in :class:`~mongoengine.ListField` or :class:`~mongoengine.DictField`)::
class ExampleFirst(Document):
# Default an empty list
values = ListField(IntField(), default=list)
class ExampleSecond(Document):
# Default a set of values
values = ListField(IntField(), default=lambda: [1,2,3])
class ExampleDangerous(Document):
# This can make an .append call to add values to the default (and all the following objects),
# instead to just an object
values = ListField(IntField(), default=[1,2,3])
:attr:`unique` (Default: False) :attr:`unique` (Default: False)
When True, no documents in the collection will have the same value for this When True, no documents in the collection will have the same value for this
field. field.