|
|
|
|
@@ -62,31 +62,31 @@ not provided. Default values may optionally be a callable, which will be called
|
|
|
|
|
to retrieve the value (such as in the above example). The field types available
|
|
|
|
|
are as follows:
|
|
|
|
|
|
|
|
|
|
* :class:`~mongoengine.BinaryField`
|
|
|
|
|
* :class:`~mongoengine.BooleanField`
|
|
|
|
|
* :class:`~mongoengine.ComplexDateTimeField`
|
|
|
|
|
* :class:`~mongoengine.DateTimeField`
|
|
|
|
|
* :class:`~mongoengine.DecimalField`
|
|
|
|
|
* :class:`~mongoengine.DictField`
|
|
|
|
|
* :class:`~mongoengine.DynamicField`
|
|
|
|
|
* :class:`~mongoengine.EmailField`
|
|
|
|
|
* :class:`~mongoengine.EmbeddedDocumentField`
|
|
|
|
|
* :class:`~mongoengine.FileField`
|
|
|
|
|
* :class:`~mongoengine.FloatField`
|
|
|
|
|
* :class:`~mongoengine.GenericEmbeddedDocumentField`
|
|
|
|
|
* :class:`~mongoengine.GenericReferenceField`
|
|
|
|
|
* :class:`~mongoengine.GeoPointField`
|
|
|
|
|
* :class:`~mongoengine.ImageField`
|
|
|
|
|
* :class:`~mongoengine.IntField`
|
|
|
|
|
* :class:`~mongoengine.ListField`
|
|
|
|
|
* :class:`~mongoengine.MapField`
|
|
|
|
|
* :class:`~mongoengine.ObjectIdField`
|
|
|
|
|
* :class:`~mongoengine.ReferenceField`
|
|
|
|
|
* :class:`~mongoengine.SequenceField`
|
|
|
|
|
* :class:`~mongoengine.SortedListField`
|
|
|
|
|
* :class:`~mongoengine.StringField`
|
|
|
|
|
* :class:`~mongoengine.URLField`
|
|
|
|
|
* :class:`~mongoengine.UUIDField`
|
|
|
|
|
* :class:`~mongoengine.fields.BinaryField`
|
|
|
|
|
* :class:`~mongoengine.fields.BooleanField`
|
|
|
|
|
* :class:`~mongoengine.fields.ComplexDateTimeField`
|
|
|
|
|
* :class:`~mongoengine.fields.DateTimeField`
|
|
|
|
|
* :class:`~mongoengine.fields.DecimalField`
|
|
|
|
|
* :class:`~mongoengine.fields.DictField`
|
|
|
|
|
* :class:`~mongoengine.fields.DynamicField`
|
|
|
|
|
* :class:`~mongoengine.fields.EmailField`
|
|
|
|
|
* :class:`~mongoengine.fields.EmbeddedDocumentField`
|
|
|
|
|
* :class:`~mongoengine.fields.FileField`
|
|
|
|
|
* :class:`~mongoengine.fields.FloatField`
|
|
|
|
|
* :class:`~mongoengine.fields.GenericEmbeddedDocumentField`
|
|
|
|
|
* :class:`~mongoengine.fields.GenericReferenceField`
|
|
|
|
|
* :class:`~mongoengine.fields.GeoPointField`
|
|
|
|
|
* :class:`~mongoengine.fields.ImageField`
|
|
|
|
|
* :class:`~mongoengine.fields.IntField`
|
|
|
|
|
* :class:`~mongoengine.fields.ListField`
|
|
|
|
|
* :class:`~mongoengine.fields.MapField`
|
|
|
|
|
* :class:`~mongoengine.fields.ObjectIdField`
|
|
|
|
|
* :class:`~mongoengine.fields.ReferenceField`
|
|
|
|
|
* :class:`~mongoengine.fields.SequenceField`
|
|
|
|
|
* :class:`~mongoengine.fields.SortedListField`
|
|
|
|
|
* :class:`~mongoengine.fields.StringField`
|
|
|
|
|
* :class:`~mongoengine.fields.URLField`
|
|
|
|
|
* :class:`~mongoengine.fields.UUIDField`
|
|
|
|
|
|
|
|
|
|
Field arguments
|
|
|
|
|
---------------
|
|
|
|
|
@@ -110,7 +110,7 @@ arguments can be set on all fields:
|
|
|
|
|
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`)::
|
|
|
|
|
(like in :class:`~mongoengine.fields.ListField` or :class:`~mongoengine.fields.DictField`)::
|
|
|
|
|
|
|
|
|
|
class ExampleFirst(Document):
|
|
|
|
|
# Default an empty list
|
|
|
|
|
@@ -172,8 +172,8 @@ arguments can be set on all fields:
|
|
|
|
|
List fields
|
|
|
|
|
-----------
|
|
|
|
|
MongoDB allows the storage of lists of items. To add a list of items to a
|
|
|
|
|
:class:`~mongoengine.Document`, use the :class:`~mongoengine.ListField` field
|
|
|
|
|
type. :class:`~mongoengine.ListField` takes another field object as its first
|
|
|
|
|
:class:`~mongoengine.Document`, use the :class:`~mongoengine.fields.ListField` field
|
|
|
|
|
type. :class:`~mongoengine.fields.ListField` takes another field object as its first
|
|
|
|
|
argument, which specifies which type elements may be stored within the list::
|
|
|
|
|
|
|
|
|
|
class Page(Document):
|
|
|
|
|
@@ -191,7 +191,7 @@ inherit from :class:`~mongoengine.EmbeddedDocument` rather than
|
|
|
|
|
content = StringField()
|
|
|
|
|
|
|
|
|
|
To embed the document within another document, use the
|
|
|
|
|
:class:`~mongoengine.EmbeddedDocumentField` field type, providing the embedded
|
|
|
|
|
:class:`~mongoengine.fields.EmbeddedDocumentField` field type, providing the embedded
|
|
|
|
|
document class as the first argument::
|
|
|
|
|
|
|
|
|
|
class Page(Document):
|
|
|
|
|
@@ -206,7 +206,7 @@ Dictionary Fields
|
|
|
|
|
Often, an embedded document may be used instead of a dictionary -- generally
|
|
|
|
|
this is recommended as dictionaries don't support validation or custom field
|
|
|
|
|
types. However, sometimes you will not know the structure of what you want to
|
|
|
|
|
store; in this situation a :class:`~mongoengine.DictField` is appropriate::
|
|
|
|
|
store; in this situation a :class:`~mongoengine.fields.DictField` is appropriate::
|
|
|
|
|
|
|
|
|
|
class SurveyResponse(Document):
|
|
|
|
|
date = DateTimeField()
|
|
|
|
|
@@ -224,7 +224,7 @@ other objects, so are the most flexible field type available.
|
|
|
|
|
Reference fields
|
|
|
|
|
----------------
|
|
|
|
|
References may be stored to other documents in the database using the
|
|
|
|
|
:class:`~mongoengine.ReferenceField`. Pass in another document class as the
|
|
|
|
|
:class:`~mongoengine.fields.ReferenceField`. Pass in another document class as the
|
|
|
|
|
first argument to the constructor, then simply assign document objects to the
|
|
|
|
|
field::
|
|
|
|
|
|
|
|
|
|
@@ -245,9 +245,9 @@ field::
|
|
|
|
|
The :class:`User` object is automatically turned into a reference behind the
|
|
|
|
|
scenes, and dereferenced when the :class:`Page` object is retrieved.
|
|
|
|
|
|
|
|
|
|
To add a :class:`~mongoengine.ReferenceField` that references the document
|
|
|
|
|
To add a :class:`~mongoengine.fields.ReferenceField` that references the document
|
|
|
|
|
being defined, use the string ``'self'`` in place of the document class as the
|
|
|
|
|
argument to :class:`~mongoengine.ReferenceField`'s constructor. To reference a
|
|
|
|
|
argument to :class:`~mongoengine.fields.ReferenceField`'s constructor. To reference a
|
|
|
|
|
document that has not yet been defined, use the name of the undefined document
|
|
|
|
|
as the constructor's argument::
|
|
|
|
|
|
|
|
|
|
@@ -325,7 +325,7 @@ Its value can take any of the following constants:
|
|
|
|
|
:const:`mongoengine.PULL`
|
|
|
|
|
Removes the reference to the object (using MongoDB's "pull" operation)
|
|
|
|
|
from any object's fields of
|
|
|
|
|
:class:`~mongoengine.ListField` (:class:`~mongoengine.ReferenceField`).
|
|
|
|
|
:class:`~mongoengine.fields.ListField` (:class:`~mongoengine.fields.ReferenceField`).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
@@ -352,7 +352,7 @@ Its value can take any of the following constants:
|
|
|
|
|
Generic reference fields
|
|
|
|
|
''''''''''''''''''''''''
|
|
|
|
|
A second kind of reference field also exists,
|
|
|
|
|
:class:`~mongoengine.GenericReferenceField`. This allows you to reference any
|
|
|
|
|
:class:`~mongoengine.fields.GenericReferenceField`. This allows you to reference any
|
|
|
|
|
kind of :class:`~mongoengine.Document`, and hence doesn't take a
|
|
|
|
|
:class:`~mongoengine.Document` subclass as a constructor argument::
|
|
|
|
|
|
|
|
|
|
@@ -376,15 +376,15 @@ kind of :class:`~mongoengine.Document`, and hence doesn't take a
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
Using :class:`~mongoengine.GenericReferenceField`\ s is slightly less
|
|
|
|
|
efficient than the standard :class:`~mongoengine.ReferenceField`\ s, so if
|
|
|
|
|
Using :class:`~mongoengine.fields.GenericReferenceField`\ s is slightly less
|
|
|
|
|
efficient than the standard :class:`~mongoengine.fields.ReferenceField`\ s, so if
|
|
|
|
|
you will only be referencing one document type, prefer the standard
|
|
|
|
|
:class:`~mongoengine.ReferenceField`.
|
|
|
|
|
:class:`~mongoengine.fields.ReferenceField`.
|
|
|
|
|
|
|
|
|
|
Uniqueness constraints
|
|
|
|
|
----------------------
|
|
|
|
|
MongoEngine allows you to specify that a field should be unique across a
|
|
|
|
|
collection by providing ``unique=True`` to a :class:`~mongoengine.Field`\ 's
|
|
|
|
|
collection by providing ``unique=True`` to a :class:`~mongoengine.fields.Field`\ 's
|
|
|
|
|
constructor. If you try to save a document that has the same value for a unique
|
|
|
|
|
field as a document that is already in the database, a
|
|
|
|
|
:class:`~mongoengine.OperationError` will be raised. You may also specify
|
|
|
|
|
@@ -492,11 +492,11 @@ Geospatial indexes
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
Geospatial indexes will be automatically created for all
|
|
|
|
|
:class:`~mongoengine.GeoPointField`\ s
|
|
|
|
|
:class:`~mongoengine.fields.GeoPointField`\ s
|
|
|
|
|
|
|
|
|
|
It is also possible to explicitly define geospatial indexes. This is
|
|
|
|
|
useful if you need to define a geospatial index on a subfield of a
|
|
|
|
|
:class:`~mongoengine.DictField` or a custom field that contains a
|
|
|
|
|
:class:`~mongoengine.fields.DictField` or a custom field that contains a
|
|
|
|
|
point. To create a geospatial index you must prefix the field with the
|
|
|
|
|
***** sign. ::
|
|
|
|
|
|
|
|
|
|
|