BaseField¶
-
class
BaseField(*, default=None, required=False, unique=False)¶ The base class for any field. Used for connecting to the parent document and calling general methods for setting and validating values.
Parameters: - default – optional (None) – Defines a default value based on the field type.
- required (bool) – optional (False) – Defines if the fields value can be None.
- unique (bool) – optional (False) – Defines if the fields value has to be unique.
Raises: TypeMismatch – Trying to set a value with the wrong type
FloatField¶
-
class
FloatField(*, default=None, required=False, unique=False)¶ Bases:
motorturbine.fields.base_field.BaseFieldThis field only allows a float type to be set as its value.
IntField¶
-
class
IntField(*, default=None, required=False, unique=False)¶ Bases:
motorturbine.fields.base_field.BaseFieldThis field only allows an int type to be set as its value.
StringField¶
-
class
StringField(*, default=None, required=False, unique=False)¶ Bases:
motorturbine.fields.base_field.BaseFieldThis field only allows a str type to be set as its value.
BooleanField¶
-
class
BooleanField(*, default=None, required=False, unique=False)¶ Bases:
motorturbine.fields.base_field.BaseFieldThis field only allows an bool type to be set as its value.
ObjectIdField¶
-
class
ObjectIdField(*, default=None, required=False, unique=False)¶ Bases:
motorturbine.fields.base_field.BaseFieldThis field only allows a
bson.ObjectIdto be set as its value.
DateTimeField¶
-
class
DateTimeField(*, default=None, required=False, unique=False)¶ Bases:
motorturbine.fields.base_field.BaseFieldThis field allows multiple types to be set as its value but will always parse them to a
datetimeobject.- Accepted types:
- str - Any accepted by
dateutil.parser.parse()(docs) - int - Unix timestamp
- float - Unix timestamp
datetime.datedatetime.datetime
- str - Any accepted by
Note
Make sure to always use UTC times when trying to insert times to avoid issues between timezones! For example use
datetime.utcnow()instead ofdatetime.now()
ReferenceField¶
-
class
ReferenceField(reference_doc, *, allow_subclass=False, default=None, required=False, unique=False)¶ Bases:
motorturbine.fields.objectid_field.ObjectIdFieldThis field allows another Document to be set as its value. A ReferenceField does not auto-insert other fields. Therefore make sure to insert them before you try to set them as a reference.
Parameters: - reference_doc (BaseDocument) – Sets the document type that will be checked when setting the reference.
- allow_subclass (bool) – optional (False) – Controls whether or not it should be possible to set instances of a subclass of the specified document as a reference.
DocumentField¶
-
class
DocumentField(embed_doc, *, default=None, required=False, unique=False)¶ Bases:
motorturbine.fields.base_field.BaseFieldThis field allows another Document to be set as its value. Any document inserted as an embedded field will be treated like an object inside of its parent. It enables to create more complex document trees than by just using
MapField.Example usage:
class Identifier(BaseDocument): serial = fields.StringField() stamp = fields.DateTimeField() location = fields.StringField() class Part(BaseDocument): name = fields.StringField() ident = fields.DocumentField(Identifier) now = datetime.utcnow() ident = Identifier(serial='9X1-33D-52A', stamp=now, location='US') part = Part(name='Xerxes', ident=ident) await part.save()
In this example an Identifier is attached to each Part that is produced. It wouldn’t have been easily possibly to create this structure by using a
MapFieldbecause the Identifier is built from more than one data types.Parameters: embed_doc (BaseDocument) – Sets the document type that will be checked when embedding an instance.
ListField¶
-
class
ListField(sub_field, *, default=[], required=False, unique=False)¶ Bases:
motorturbine.fields.base_field.BaseFieldThis field only allows a list type to be set as its value.
If an entire list is set instead of singular values each entry in the new list has to match the subfield that was set when initialising the field.
Parameters: sub_field (BaseField) – Sets the field type that will be used for the entires of the list.
MapField¶
-
class
MapField(value_field, key_field=StringField(), *, default={}, required=False, unique=False)¶ Bases:
motorturbine.fields.base_field.BaseFieldThis field only allows a dict type to be set as its value.
If an entire dict is set instead of singular values each key-value pair in the new dict has to match the subfields that were set when initialising the field.
Parameters: value_field (BaseField) – Sets the field type that will be used for the values of the dict.