rest_framework_json_api.serializers module

class rest_framework_json_api.serializers.ResourceIdentifierObjectSerializer(*args, **kwargs)

Bases: BaseSerializer

default_error_messages = {'does_not_exist': 'Invalid pk "{pk_value}" - object does not exist.', 'incorrect_model_type': 'Incorrect model type. Expected {model_type}, received {received_type}.', 'incorrect_type': 'Incorrect type. Expected pk value, received {data_type}.'}
model_class = None
to_representation(instance)
to_internal_value(data)
class rest_framework_json_api.serializers.SparseFieldsetsMixin(*args, **kwargs)

Bases: object

A serializer mixin that adds support for sparse fieldsets through fields query parameter.

Specification: https://jsonapi.org/format/#fetching-sparse-fieldsets

class rest_framework_json_api.serializers.IncludedResourcesValidationMixin(*args, **kwargs)

Bases: object

A serializer mixin that adds validation of include query parameter to support compound documents.

Specification: https://jsonapi.org/format/#document-compound-documents)

class rest_framework_json_api.serializers.ReservedFieldNamesMixin

Bases: object

Ensures that reserved field names are not used and an error raised instead.

get_fields()
class rest_framework_json_api.serializers.LazySerializersDict(parent, serializers)

Bases: Mapping

A dictionary of serializers which lazily import dotted class path and self.

class rest_framework_json_api.serializers.SerializerMetaclass(name, bases, attrs)

Bases: SerializerMetaclass

class rest_framework_json_api.serializers.Serializer(*args, **kwargs)

Bases: IncludedResourcesValidationMixin, SparseFieldsetsMixin, ReservedFieldNamesMixin, Serializer

A Serializer is a model-less serializer class with additional support for JSON:API spec features.

As in JSON:API specification a type is always required you need to make sure that you define resource_name in your Meta class when deriving from this class.

Included Mixins:

  • A mixin class to enable sparse fieldsets is included

  • A mixin class to enable validation of included resources is included

class rest_framework_json_api.serializers.HyperlinkedModelSerializer(*args, **kwargs)

Bases: IncludedResourcesValidationMixin, SparseFieldsetsMixin, ReservedFieldNamesMixin, HyperlinkedModelSerializer

A type of ModelSerializer that uses hyperlinked relationships instead of primary key relationships. Specifically:

  • A ‘url’ field is included instead of the ‘id’ field.

  • Relationships to other instances are hyperlinks, instead of primary keys.

Included Mixins:

  • A mixin class to enable sparse fieldsets is included

  • A mixin class to enable validation of included resources is included

class rest_framework_json_api.serializers.ModelSerializer(*args, **kwargs)

Bases: IncludedResourcesValidationMixin, SparseFieldsetsMixin, ReservedFieldNamesMixin, ModelSerializer

A ModelSerializer is just a regular Serializer, except that:

  • A set of default fields are automatically populated.

  • A set of default validators are automatically populated.

  • Default .create() and .update() implementations are provided.

The process of automatically determining a set of serializer fields based on the model fields is reasonably complex, but you almost certainly don’t need to dig into the implementation.

If the ModelSerializer class doesn’t generate the set of fields that you need you should either declare the extra/differing fields explicitly on the serializer class, or simply use a Serializer class.

Included Mixins:

  • A mixin class to enable sparse fieldsets is included

  • A mixin class to enable validation of included resources is included

alias of ResourceRelatedField

get_field_names(declared_fields, info)

We override the parent to omit explicity defined meta fields (such as SerializerMethodFields) from the list of declared fields

class rest_framework_json_api.serializers.PolymorphicSerializerMetaclass(name, bases, attrs)

Bases: SerializerMetaclass

This metaclass ensures that the polymorphic_serializers is correctly defined on a PolymorphicSerializer class and make a cache of model/serializer/type mappings.

class rest_framework_json_api.serializers.PolymorphicModelSerializer(*args, **kwargs)

Bases: ModelSerializer

A serializer for polymorphic models. Useful for “lazy” parent models. Leaves should be represented with a regular serializer.

get_fields()

Return an exhaustive list of the polymorphic serializer fields.

classmethod get_polymorphic_serializer_for_instance(instance)

Return the polymorphic serializer associated with the given instance/model. Raise NotImplementedError if no serializer is found for the given model. This usually means that a serializer is missing in the class’s polymorphic_serializers attribute.

classmethod get_polymorphic_model_for_serializer(serializer)

Return the polymorphic model associated with the given serializer. Raise NotImplementedError if no model is found for the given serializer. This usually means that a serializer is missing in the class’s polymorphic_serializers attribute.

classmethod get_polymorphic_serializer_for_type(obj_type)

Return the polymorphic serializer associated with the given type. Raise NotImplementedError if no serializer is found for the given type. This usually means that a serializer is missing in the class’s polymorphic_serializers attribute.

classmethod get_polymorphic_model_for_type(obj_type)

Return the polymorphic model associated with the given type. Raise NotImplementedError if no model is found for the given type. This usually means that a serializer is missing in the class’s polymorphic_serializers attribute.

classmethod get_polymorphic_types()

Return the list of accepted types.

to_representation(instance)

Retrieve the appropriate polymorphic serializer and use this to handle representation.

to_internal_value(data)

Ensure that the given type is one of the expected polymorphic types, then retrieve the appropriate polymorphic serializer and use this to handle internal value.