Fields

class nefertari_mongodb.fields.IntegerField(*args, **kwargs)
class nefertari_mongodb.fields.BigIntegerField(*args, **kwargs)
class nefertari_mongodb.fields.SmallIntegerField(*args, **kwargs)

As mongoengine does not provide SmallInt, this is just a copy of IntegerField.

class nefertari_mongodb.fields.BooleanField(*args, **kwargs)
class nefertari_mongodb.fields.DateField(*args, **kwargs)

Custom field that stores datetime.date instances.

This is basically mongoengine’s DateTimeField which gets datetime’s .date() and formats it into a string before storing to mongo.

to_mongo(*args, **kwargs)

Override mongo’s DateTimeField value conversion to get date instead of datetime.

class nefertari_mongodb.fields.DateTimeField(*args, **kwargs)
class nefertari_mongodb.fields.FloatField(*args, **kwargs)
validate(value)

Override to try to convert string values to floats.

class nefertari_mongodb.fields.StringField(*args, **kwargs)
class nefertari_mongodb.fields.TextField(*args, **kwargs)
class nefertari_mongodb.fields.UnicodeField(*args, **kwargs)
class nefertari_mongodb.fields.UnicodeTextField(*args, **kwargs)
class nefertari_mongodb.fields.ChoiceField(*args, **kwargs)

As mongoengine does not have an explicit ChoiceField, but all mongoengine fields accept choices kwarg, we need to define a proxy here. It uses a naive aproach: check the type of first choice and instantiate a field of an appropriate type under the hood. Then translate all the attribute access to the underlying field.

class nefertari_mongodb.fields.BinaryField(*args, **kwargs)
translate_kwargs(kwargs)

Translate kwargs from one key to another.

Translates:
length -> max_bytes
class nefertari_mongodb.fields.DecimalField(*args, **kwargs)

This is basically a DecimalField with a fixed name of precision kwarg.

translate_kwargs(kwargs)

Translate kwargs from one key to another.

Translates:
scale -> precision
class nefertari_mongodb.fields.TimeField(*args, **kwargs)

Custom field that stores datetime.date instances.

class nefertari_mongodb.fields.PickleField(*args, **kwargs)

Custom field that stores pickled data as a BinaryField.

Data is pickled when saving to mongo and unpickled when retrieving from mongo. The pickler kwarg may be provided that may reference any object with pickle-compatible dumps and loads methods. Defaults to python’s built-in pickle.

class nefertari_mongodb.fields.IntervalField(*args, **kwargs)

Custom field that stores datetime.timedelta instances.

Values are stored as seconds in mongo and loaded by `datetime.timedelta(seconds=<value>) when restoring from mongo.

class nefertari_mongodb.fields.ListField(*args, **kwargs)

Custom ListField.

The custom part is the validation. Choices are stored in a separate attribute :self.list_choices: and validation checks if the value (which is a sequence) contains anything other than the choices specified in :self.list_choices:.

The original mongoengine ListField validation requires the value to be a sequence but checks for value(sequence) inclusion in choices.

class nefertari_mongodb.fields.DictField(*args, **kwargs)
class nefertari_mongodb.fields.IdField(*args, **kwargs)

Just a subclass of ObjectIdField that must be used for fields that represent database-specific ‘id’ field.

class nefertari_mongodb.fields.ForeignKeyField(*args, **kwargs)

Field that references another document.

It is not meant to be used inside the mongodb engine. It is added for compatibility with sqla and is not displayed in JSON output.

class nefertari_mongodb.fields.ReferenceField(*args, **kwargs)

Field that references another document.

It ISN’T MEANT to be used explicitly by the user. To create a relationship field, use the `Relationship` constructor function.

When this field is not added to a model’s _nested_relationships, this field returns an ID of the document that is being referenced. Otherwise the full document is included in JSON response.

This class is used in a Relationship function to generate a kind of one-to-one relationship. It is also used to create backreferences.

reverse_rel_field: string name of a field on the related document.
Used when generating backreferences so that fields on each side know the name of the field on the other side.
__init__(*args, **kwargs)

Init the field.

Also saves backref kwargs for future creation of the backref.

Expects:
document or <_kwargs_prefix>document: mongoengine model name.
__set__(instance, value)

Custom __set__ method that updates linked relationships.

Updates linked relationship fields if the current field has a reverse_rel_field property set. By default this property is only set when the backreference is created.

If value is changed, instance is deleted from object it was related to before and is added to object it will be related to now - value.

_get_referential_action(kwargs)

Determine/translate generic rule name to mongoengine-specific rule.

Custom rule names are used here to make them look SQL-ish and pretty at the same time.

Mongoengine rules are:
DO_NOTHING Don’t do anything (default) NULLIFY Updates the reference to null CASCADE Deletes the documents associated with the reference DENY Prevent the deletion of the reference object PULL Pull the reference from a ListField of references
_register_addition_hook(new_object, instance)

Register backref hook to add instance to the new_object`s field to which `instance became related by the backref.

instance is either added to new_object field’s collection or new_object`s field, responsible for relationship is set to `instance. This depends on type of the field at new_object.

instance is not actually used in hook - up-to-date value of instance is passed to hook when it is run.

_register_deletion_hook(old_object, instance)

Register a backref hook to delete the instance from the old_object‘s field to which the instance was related before by the backref.

instance is either deleted from the old_object field’s collection or old_object‘s field, responsible for relationship is set to None. This depends on type of the field at old_object.

instance is not actually used in hook - up-to-date value of instance is passed to hook when it is run.

class nefertari_mongodb.fields.RelationshipField(*args, **kwargs)

Relationship field meant to be used to create one-to-many relationships.

It ISN’T MEANT to be used by users explicitly. To create a relationship field, use the `Relationship` constructor function.

It is used in the Relationship function to generate one-to-many relationships. Under the hood it is just a ListField containing ReferenceFields.

reverse_rel_field: string name of a field on the related document.
Used when generating backreferences so that fields on each side know the name of the field on the other side.
__init__(*args, **kwargs)

Save backref kwargs for future creation of backref.

__set__(instance, value)

Custom __set__ method that updates linked relationships.

Updates linked relationship fields if current field has reverse_rel_field property set. By default this property is only set when a backreference is created.

If the value is changed, instance is deleted from the object it was related to before and is added to the object it will be related to now - value.

_register_addition_hook(new_object, instance)

Define and register addition hook.

Hook sets instance at new_object using self.reverse_rel_field as a field which should be set. instance is not actually used in hook - up-to-date value of instance is passed to hook when it is run.

_register_deletion_hook(old_object, instance)

Define and register deletion hook.

Hook removes instance from new_object using self.reverse_rel_field as a field from which value should be removed. instance is not actually used in hook - up-to-date value of instance is passed to hook when it is run.

translate_kwargs(kwargs)

For RelationshipField use ReferenceField keys without prefix.

class nefertari_mongodb.fields.Relationship

Relationship field generator.

Should be used to generate one-to-many and one-to-one relationships. Provide uselist to indicate which kind of relation you expect to get. If uselist is True, then RelationshipField is used and a one-to-many is created. Otherwise ReferenceField is used and a one-to-one is created.

This is the place where ondelete rules kwargs should be passed. If you switched from the SQLA engine, copy here the same ondelete rules you passed to SQLA’s ForeignKeyField. ondelete kwargs may be kept in both fields with no side-effects when switching between the sqla and mongo engines.