user
View Source
import mongoengine from place import Region, Country, Cap # User document class User(mongoengine.Document): _id = mongoengine.StringField(primary_key=True, required=True) # telegram chat id health_card = mongoengine.StringField(required=True) # health card number fiscal_code = mongoengine.StringField(required=True) # fiscal code region = mongoengine.ReferenceField(Region, required=True) # referred region country = mongoengine.ReferenceField(Country, required=True) # referred country postal_code = mongoengine.ReferenceField(Cap, required=True) # referred CAP date = mongoengine.StringField(required=True) # birthdate phone = mongoengine.StringField(required=True) # phone number is_vaccinated = mongoengine.BooleanField(default=False) # is vaccinated flag appointments_by_distance = mongoengine.ListField(mongoengine.DictField(), default=[]) # appoinments list by distance appointments_by_date = mongoengine.ListField(mongoengine.DictField(), default=[]) # appointments list by date last_fetch = mongoengine.StringField(default="") # last time appointments were fetched is_booking = mongoengine.BooleanField(default=False) # is booking flag
View Source
class User(mongoengine.Document): _id = mongoengine.StringField(primary_key=True, required=True) # telegram chat id health_card = mongoengine.StringField(required=True) # health card number fiscal_code = mongoengine.StringField(required=True) # fiscal code region = mongoengine.ReferenceField(Region, required=True) # referred region country = mongoengine.ReferenceField(Country, required=True) # referred country postal_code = mongoengine.ReferenceField(Cap, required=True) # referred CAP date = mongoengine.StringField(required=True) # birthdate phone = mongoengine.StringField(required=True) # phone number is_vaccinated = mongoengine.BooleanField(default=False) # is vaccinated flag appointments_by_distance = mongoengine.ListField(mongoengine.DictField(), default=[]) # appoinments list by distance appointments_by_date = mongoengine.ListField(mongoengine.DictField(), default=[]) # appointments list by date last_fetch = mongoengine.StringField(default="") # last time appointments were fetched is_booking = mongoengine.BooleanField(default=False) # is booking flag
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 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)
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 unicode string field.
A unicode string field.
Boolean field type.
A list field that wraps a standard field, allowing multiple instances of the field to be used as a list in the database.
If using with ReferenceFields see: :ref:many-to-many-with-listfields
Note: Required means it cannot be empty - as the default for ListFields is []
A list field that wraps a standard field, allowing multiple instances of the field to be used as a list in the database.
If using with ReferenceFields see: :ref:many-to-many-with-listfields
Note: Required means it cannot be empty - as the default for ListFields is []
A unicode string field.
Boolean field type.
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 unicode string field.
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