Fields

These are some fields specific to mongoengine, or used internally by the serializers.

The fields that are derived from fields.DocumentField are dependend on underlying mongoengine field and used internally by serializers.DocumentSerializer. Other fields can be used in ordinary DRF serializer.

GenericField

class rest_framework_mongoengine.fields.GenericField(**kwargs)

Bases: rest_framework.fields.Field

Field for generic values.

Recursively traverses lists and dicts. Primitive values are serialized using django.utils.encoding.smart_text (keeping json-safe intact). Embedded documents handled using temporary GenericEmbeddedField.

No validation performed.

Note: it will not work properly if a value contains some complex elements.

GeoPointField

class rest_framework_mongoengine.fields.GeoPointField(**kwargs)

Bases: rest_framework_mongoengine.fields.MongoValidatingField, rest_framework.fields.Field

Field for 2D point values.

Internal value and representation: [ x, y ]

Validation is delegated to mongoengine field.

mongo_field

alias of GeoPointField

GeoJSONField

class rest_framework_mongoengine.fields.GeoJSONField(geo_type, *args, **kwargs)

Bases: rest_framework_mongoengine.fields.MongoValidatingField, rest_framework.fields.Field

Field for GeoJSON values.

Shouldbe specified with argument geo_type referencing to GeoJSON geometry type (‘Point’, ‘LineSting’, etc)

Internal value: [ coordinates ] (as required by mongoengine fields).

Representation: { 'type': str, 'coordinates': [ coords ] } (GeoJSON geometry format).

Validation: delegated to corresponding mongoengine field.

ObjectIdField

class rest_framework_mongoengine.fields.ObjectIdField(**kwargs)

Bases: rest_framework.fields.Field

Field for ObjectId values

ReferenceField

class rest_framework_mongoengine.fields.ReferenceField(model=None, queryset=None, **kwargs)

Bases: rest_framework.fields.Field

Field for References.

Should be specified with model or queryset argument pointing to referenced model.

Internal value: DBRef.

Representation: str(id), or { _id: str(id) } (for compatibility with GenericReference).

Validation checks existance of referenced object.

pk_field_class

Serializer field class used to handle object ids.

If your docments have another type for ids, you need to create custom ReferenceField class and override this atribute to corresponding serializer field. However, this custom ReferenceField class will not be used automagically by DocumentSerializer, and you have to declare fields manually.

alias of ObjectIdField

GenericReferenceField

class rest_framework_mongoengine.fields.GenericReferenceField(**kwargs)

Bases: rest_framework.fields.Field

Field for GenericReferences.

Internal value: Document, retrieved with only id field. The mongengine does not support DBRef here.

Representation: { _cls: str, _id: str }.

Validation checks existance of given class and existance of referenced model.

pk_field_class

The same as for ReferenceField

alias of ObjectIdField

GenericEmbeddedField

class rest_framework_mongoengine.fields.GenericEmbeddedField(**kwargs)

Bases: rest_framework.fields.Field

Field for generic embedded documents.

Serializes like DictField with additional item _cls.

DocumentField

class rest_framework_mongoengine.fields.DocumentField(model_field, **kwargs)

Bases: rest_framework.fields.Field

Replacement of DRF ModelField.

Keeps track of underlying mognoengine field.

Used by DocumentSerializers to map unknown fields.

NB: This is not DocumentField from previous releases. For previous behaviour see GenericField

run_validators(value)

validate value.

Uses document field’s validate()

to_internal_value(data)

convert input to python value.

Uses document field’s to_python().

to_representation(obj)

convert value to representation.

DRF ModelField uses value_to_string for this purpose. Mongoengine fields do not have such method.

This implementation uses django.utils.encoding.smart_text to convert everything to text, while keeping json-safe types intact.

NB: The argument is whole object, instead of attribute value. This is upstream feature. Probably because the field can be represented by a complicated method with nontrivial way to extract data.

DynamicField

class rest_framework_mongoengine.fields.DynamicField(model_field, **kwargs)

Bases: rest_framework_mongoengine.fields.GenericField, rest_framework_mongoengine.fields.AttributedDocumentField

Field for DynamicDocuments.

Used internally by DynamicDocumentSerializer.

GenericEmbeddedDocumentField

class rest_framework_mongoengine.fields.GenericEmbeddedDocumentField(model_field, **kwargs)

Bases: rest_framework_mongoengine.fields.GenericEmbeddedField, rest_framework_mongoengine.fields.AttributedDocumentField

Field for GenericEmbeddedDocumentField.

Used internally by DocumentSerializer.