place
View Source
import mongoengine # Region document class Region(mongoengine.Document): name = mongoengine.StringField(required=True) # region name # CAP document class Cap(mongoengine.Document): number = mongoengine.StringField(required=True, min_length=5, max_length=5) # CAP number region = mongoengine.ReferenceField(Region, required=True) # referred region # Country document class Country(mongoengine.Document): name = mongoengine.StringField(required=True) # country name region = mongoengine.ReferenceField(Region, required=True) # referred region postal_code = mongoengine.ReferenceField(Cap, required=True) # referred CAP
View Source
class Region(mongoengine.Document): name = mongoengine.StringField(required=True) # region name
The base class used for defining the structure and properties of
collections of documents stored in MongoDB. Inherit from this class, and
add fields as class attributes to define a document's structure.
Individual documents may then be created by making instances of the
~mongoengine.Document
subclass.
By default, the MongoDB collection used to store documents created using a
~mongoengine.Document
subclass will be the name of the subclass
converted to snake_case. A different collection may be specified by
providing collection
to the meta
dictionary in the class
definition.
A ~mongoengine.Document
subclass may be itself subclassed, to
create a specialised version of the document that will be stored in the
same collection. To facilitate this behaviour a _cls
field is added to documents (hidden though the MongoEngine interface).
To enable this behaviourset allow_inheritance
to True
in the
meta
dictionary.
A ~mongoengine.Document
may use a Capped Collection by
specifying max_documents
and max_size
in the meta
dictionary. max_documents
is the maximum number of documents that
is allowed to be stored in the collection, and max_size
is the
maximum size of the collection in bytes. max_size
is rounded up
to the next multiple of 256 by MongoDB internally and mongoengine before.
Use also a multiple of 256 to avoid confusions. If max_size
is not
specified and max_documents
is, max_size
defaults to
10485760 bytes (10MB).
Indexes may be created by specifying indexes
in the meta
dictionary. The value should be a list of field names or tuples of field
names. Index direction may be specified by prefixing the field names with
a + or - sign.
Automatic index creation can be disabled by specifying
auto_create_index
in the meta
dictionary. If this is set to
False then indexes will not be created by MongoEngine. This is useful in
production systems where index creation is performed as part of a
deployment system.
By default, _cls will be added to the start of every index (that doesn't contain a list) if allow_inheritance is True. This can be disabled by either setting cls to False on the specific index or by setting index_cls to False on the meta dictionary for the document.
By default, any extra attribute existing in stored data but not declared
in your model will raise a ~mongoengine.FieldDoesNotExist
error.
This can be disabled by setting strict
to False
in the meta
dictionary.
A unicode string field.
The default QuerySet Manager.
Custom QuerySet Manager functions can extend this class and users can
add extra queryset functionality. Any custom manager methods must accept a
~mongoengine.Document
class as its first argument, and a
~mongoengine.queryset.QuerySet
as its second argument.
The method function should return a ~mongoengine.queryset.QuerySet
, probably the same one that was passed in, but modified in some way.
A field wrapper around MongoDB's ObjectIds.
Inherited Members
- mongoengine.base.document.BaseDocument
- BaseDocument
- STRICT
- clean
- get_text_score
- validate
- to_json
- from_json
- mongoengine.document.Document
- my_metaclass
- pk
- to_mongo
- modify
- save
- cascade_save
- update
- delete
- switch_db
- switch_collection
- reload
- to_dbref
- register_delete_rule
- drop_collection
- create_index
- ensure_index
- ensure_indexes
- list_indexes
- compare_indexes
Common base class for all non-exit exceptions.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- args
Common base class for all non-exit exceptions.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- args
View Source
class Cap(mongoengine.Document): number = mongoengine.StringField(required=True, min_length=5, max_length=5) # CAP number region = mongoengine.ReferenceField(Region, required=True) # referred region
The base class used for defining the structure and properties of
collections of documents stored in MongoDB. Inherit from this class, and
add fields as class attributes to define a document's structure.
Individual documents may then be created by making instances of the
~mongoengine.Document
subclass.
By default, the MongoDB collection used to store documents created using a
~mongoengine.Document
subclass will be the name of the subclass
converted to snake_case. A different collection may be specified by
providing collection
to the meta
dictionary in the class
definition.
A ~mongoengine.Document
subclass may be itself subclassed, to
create a specialised version of the document that will be stored in the
same collection. To facilitate this behaviour a _cls
field is added to documents (hidden though the MongoEngine interface).
To enable this behaviourset allow_inheritance
to True
in the
meta
dictionary.
A ~mongoengine.Document
may use a Capped Collection by
specifying max_documents
and max_size
in the meta
dictionary. max_documents
is the maximum number of documents that
is allowed to be stored in the collection, and max_size
is the
maximum size of the collection in bytes. max_size
is rounded up
to the next multiple of 256 by MongoDB internally and mongoengine before.
Use also a multiple of 256 to avoid confusions. If max_size
is not
specified and max_documents
is, max_size
defaults to
10485760 bytes (10MB).
Indexes may be created by specifying indexes
in the meta
dictionary. The value should be a list of field names or tuples of field
names. Index direction may be specified by prefixing the field names with
a + or - sign.
Automatic index creation can be disabled by specifying
auto_create_index
in the meta
dictionary. If this is set to
False then indexes will not be created by MongoEngine. This is useful in
production systems where index creation is performed as part of a
deployment system.
By default, _cls will be added to the start of every index (that doesn't contain a list) if allow_inheritance is True. This can be disabled by either setting cls to False on the specific index or by setting index_cls to False on the meta dictionary for the document.
By default, any extra attribute existing in stored data but not declared
in your model will raise a ~mongoengine.FieldDoesNotExist
error.
This can be disabled by setting strict
to False
in the meta
dictionary.
A unicode string field.
A reference to a document that will be automatically dereferenced on access (lazily).
Note this means you will get a database I/O access everytime you access
this field. This is necessary because the field returns a ~mongoengine.Document
which precise type can depend of the value of the _cls
field present in the
document in database.
In short, using this type of field can lead to poor performances (especially
if you access this field only to retrieve it pk
field which is already
known before dereference). To solve this you should consider using the
~mongoengine.fields.LazyReferenceField
.
Use the reverse_delete_rule
to handle what should happen if the document
the field is referencing is deleted. EmbeddedDocuments, DictFields and
MapFields does not support reverse_delete_rule and an InvalidDocumentError
will be raised if trying to set on one of these Document / Field types.
The options are:
- DO_NOTHING (0) - don't do anything (default).
- NULLIFY (1) - Updates the reference to null.
- CASCADE (2) - Deletes the documents associated with the reference.
- DENY (3) - Prevent the deletion of the reference object.
- PULL (4) - Pull the reference from a
~mongoengine.fields.ListField
of references
Alternative syntax for registering delete rules (useful when implementing bi-directional delete rules)
.. code-block:: python
class Org(Document):
owner = ReferenceField('User')
class User(Document):
org = ReferenceField('Org', reverse_delete_rule=CASCADE)
User.register_delete_rule(Org, 'owner', DENY)
The default QuerySet Manager.
Custom QuerySet Manager functions can extend this class and users can
add extra queryset functionality. Any custom manager methods must accept a
~mongoengine.Document
class as its first argument, and a
~mongoengine.queryset.QuerySet
as its second argument.
The method function should return a ~mongoengine.queryset.QuerySet
, probably the same one that was passed in, but modified in some way.
A field wrapper around MongoDB's ObjectIds.
Inherited Members
- mongoengine.base.document.BaseDocument
- BaseDocument
- STRICT
- clean
- get_text_score
- validate
- to_json
- from_json
- mongoengine.document.Document
- my_metaclass
- pk
- to_mongo
- modify
- save
- cascade_save
- update
- delete
- switch_db
- switch_collection
- reload
- to_dbref
- register_delete_rule
- drop_collection
- create_index
- ensure_index
- ensure_indexes
- list_indexes
- compare_indexes
Common base class for all non-exit exceptions.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- args
Common base class for all non-exit exceptions.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- args
View Source
class Country(mongoengine.Document): name = mongoengine.StringField(required=True) # country name region = mongoengine.ReferenceField(Region, required=True) # referred region postal_code = mongoengine.ReferenceField(Cap, required=True) # referred CAP
The base class used for defining the structure and properties of
collections of documents stored in MongoDB. Inherit from this class, and
add fields as class attributes to define a document's structure.
Individual documents may then be created by making instances of the
~mongoengine.Document
subclass.
By default, the MongoDB collection used to store documents created using a
~mongoengine.Document
subclass will be the name of the subclass
converted to snake_case. A different collection may be specified by
providing collection
to the meta
dictionary in the class
definition.
A ~mongoengine.Document
subclass may be itself subclassed, to
create a specialised version of the document that will be stored in the
same collection. To facilitate this behaviour a _cls
field is added to documents (hidden though the MongoEngine interface).
To enable this behaviourset allow_inheritance
to True
in the
meta
dictionary.
A ~mongoengine.Document
may use a Capped Collection by
specifying max_documents
and max_size
in the meta
dictionary. max_documents
is the maximum number of documents that
is allowed to be stored in the collection, and max_size
is the
maximum size of the collection in bytes. max_size
is rounded up
to the next multiple of 256 by MongoDB internally and mongoengine before.
Use also a multiple of 256 to avoid confusions. If max_size
is not
specified and max_documents
is, max_size
defaults to
10485760 bytes (10MB).
Indexes may be created by specifying indexes
in the meta
dictionary. The value should be a list of field names or tuples of field
names. Index direction may be specified by prefixing the field names with
a + or - sign.
Automatic index creation can be disabled by specifying
auto_create_index
in the meta
dictionary. If this is set to
False then indexes will not be created by MongoEngine. This is useful in
production systems where index creation is performed as part of a
deployment system.
By default, _cls will be added to the start of every index (that doesn't contain a list) if allow_inheritance is True. This can be disabled by either setting cls to False on the specific index or by setting index_cls to False on the meta dictionary for the document.
By default, any extra attribute existing in stored data but not declared
in your model will raise a ~mongoengine.FieldDoesNotExist
error.
This can be disabled by setting strict
to False
in the meta
dictionary.
A unicode string field.
A reference to a document that will be automatically dereferenced on access (lazily).
Note this means you will get a database I/O access everytime you access
this field. This is necessary because the field returns a ~mongoengine.Document
which precise type can depend of the value of the _cls
field present in the
document in database.
In short, using this type of field can lead to poor performances (especially
if you access this field only to retrieve it pk
field which is already
known before dereference). To solve this you should consider using the
~mongoengine.fields.LazyReferenceField
.
Use the reverse_delete_rule
to handle what should happen if the document
the field is referencing is deleted. EmbeddedDocuments, DictFields and
MapFields does not support reverse_delete_rule and an InvalidDocumentError
will be raised if trying to set on one of these Document / Field types.
The options are:
- DO_NOTHING (0) - don't do anything (default).
- NULLIFY (1) - Updates the reference to null.
- CASCADE (2) - Deletes the documents associated with the reference.
- DENY (3) - Prevent the deletion of the reference object.
- PULL (4) - Pull the reference from a
~mongoengine.fields.ListField
of references
Alternative syntax for registering delete rules (useful when implementing bi-directional delete rules)
.. code-block:: python
class Org(Document):
owner = ReferenceField('User')
class User(Document):
org = ReferenceField('Org', reverse_delete_rule=CASCADE)
User.register_delete_rule(Org, 'owner', DENY)
A reference to a document that will be automatically dereferenced on access (lazily).
Note this means you will get a database I/O access everytime you access
this field. This is necessary because the field returns a ~mongoengine.Document
which precise type can depend of the value of the _cls
field present in the
document in database.
In short, using this type of field can lead to poor performances (especially
if you access this field only to retrieve it pk
field which is already
known before dereference). To solve this you should consider using the
~mongoengine.fields.LazyReferenceField
.
Use the reverse_delete_rule
to handle what should happen if the document
the field is referencing is deleted. EmbeddedDocuments, DictFields and
MapFields does not support reverse_delete_rule and an InvalidDocumentError
will be raised if trying to set on one of these Document / Field types.
The options are:
- DO_NOTHING (0) - don't do anything (default).
- NULLIFY (1) - Updates the reference to null.
- CASCADE (2) - Deletes the documents associated with the reference.
- DENY (3) - Prevent the deletion of the reference object.
- PULL (4) - Pull the reference from a
~mongoengine.fields.ListField
of references
Alternative syntax for registering delete rules (useful when implementing bi-directional delete rules)
.. code-block:: python
class Org(Document):
owner = ReferenceField('User')
class User(Document):
org = ReferenceField('Org', reverse_delete_rule=CASCADE)
User.register_delete_rule(Org, 'owner', DENY)
The default QuerySet Manager.
Custom QuerySet Manager functions can extend this class and users can
add extra queryset functionality. Any custom manager methods must accept a
~mongoengine.Document
class as its first argument, and a
~mongoengine.queryset.QuerySet
as its second argument.
The method function should return a ~mongoengine.queryset.QuerySet
, probably the same one that was passed in, but modified in some way.
A field wrapper around MongoDB's ObjectIds.
Inherited Members
- mongoengine.base.document.BaseDocument
- BaseDocument
- STRICT
- clean
- get_text_score
- validate
- to_json
- from_json
- mongoengine.document.Document
- my_metaclass
- pk
- to_mongo
- modify
- save
- cascade_save
- update
- delete
- switch_db
- switch_collection
- reload
- to_dbref
- register_delete_rule
- drop_collection
- create_index
- ensure_index
- ensure_indexes
- list_indexes
- compare_indexes
Common base class for all non-exit exceptions.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- args
Common base class for all non-exit exceptions.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- args