class documentation

Mixin for documents that can be serialized in BSON or JSON format.

Class Method from_dict Loads the document from a BSON/JSON dictionary.
Class Method from_json Loads the document from a JSON string.
Method __eq__ Undocumented
Method __repr__ Undocumented
Method __str__ Undocumented
Method clear_field Clears the field from the document.
Method copy Returns a deep copy of the document.
Method fancy_repr Generates a customizable string representation of the document.
Method get_field Gets the field of the document.
Method has_field Determines whether the document has a field of the given name.
Method iter_fields Returns an iterator over the (name, value) pairs of the public fields of the document.
Method merge Merges the contents of the given document into this document.
Method set_field Sets the value of a field of the document.
Method to_dict Serializes this document to a BSON/JSON dictionary.
Method to_json Serializes the document to a JSON string.
Property field_names An ordered tuple of the public fields of this document.
Method _get_field_names Returns an ordered tuple of field names of this document.
Method _get_repr_fields Returns an ordered tuple of field names that should be included in the repr of the document.
@classmethod
def from_dict(cls, d, extended=False): (source)

Loads the document from a BSON/JSON dictionary.

Parameters
da dictionary
extended:Falsewhether the input dictionary may contain serialized extended JSON constructs
Returns
a SerializableDocument
@classmethod
def from_json(cls, s): (source)

Loads the document from a JSON string.

Returns
a SerializableDocument
def __eq__(self, other): (source)

Undocumented

def __repr__(self): (source)

Undocumented

def __str__(self): (source)

Undocumented

def clear_field(self, field_name): (source)

Clears the field from the document.

Parameters
field_namethe field name
Raises
ValueErrorif the field does not exist
def copy(self): (source)

Returns a deep copy of the document.

Returns
a SerializableDocument
def fancy_repr(self, class_name=None, select_fields=None, exclude_fields=None, **kwargs): (source)

Generates a customizable string representation of the document.

Parameters
class_name:Noneoptional class name to use
select_fields:Noneiterable of field names to restrict to
exclude_fields:Noneiterable of field names to exclude
**kwargsadditional key-value pairs to include in the string representation
Returns
a string representation of the document
def get_field(self, field_name): (source)

Gets the field of the document.

Parameters
field_namethe field name
Returns
the field value
Raises
AttributeErrorif the field does not exist
def has_field(self, field_name): (source)

Determines whether the document has a field of the given name.

Parameters
field_namethe field name
Returns
True/False
def iter_fields(self): (source)

Returns an iterator over the (name, value) pairs of the public fields of the document.

Returns
an iterator that emits (name, value) tuples
def merge(self, doc, merge_lists=True, merge_dicts=True, overwrite=True): (source)

Merges the contents of the given document into this document.

Parameters
doca SerializableDocument of same type as this document
merge_lists:Truewhether to merge the elements of top-level list fields rather than treating the list as a single value
merge_dicts:Truewhether to recursively merge the contents of top-level dict fields rather than treating the dict as a single value
overwrite:Truewhether to overwrite (True) or skip (False) existing fields
def set_field(self, field_name, value, create=True): (source)

Sets the value of a field of the document.

Parameters
field_namethe field name
valuethe field value
create:Truewhether to create the field if it does not exist
Raises
ValueErrorif field_name is not an allowed field name or does not exist and create == False
def to_dict(self, extended=False): (source)

Serializes this document to a BSON/JSON dictionary.

Parameters
extended:Falsewhether to serialize extended JSON constructs such as ObjectIDs, Binary, etc. into JSON format
Returns
a dict
def to_json(self, pretty_print=False): (source)

Serializes the document to a JSON string.

Parameters
pretty_print:Falsewhether to render the JSON in human readable format with newlines and indentations
Returns
a JSON string
@property
field_names = (source)

An ordered tuple of the public fields of this document.

def _get_field_names(self, include_private=False, use_db_fields=False): (source)

Returns an ordered tuple of field names of this document.

Parameters
include_private:Falsewhether to include private fields
use_db_fields:Falsewhether to return database fields
Returns
a tuple of field names
def _get_repr_fields(self): (source)

Returns an ordered tuple of field names that should be included in the repr of the document.

Returns
a tuple of field names