rest_framework_json_api.serializers module

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

Bases: rest_framework.serializers.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

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

Bases: object

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

Bases: rest_framework_json_api.serializers.IncludedResourcesValidationMixin, rest_framework_json_api.serializers.SparseFieldsetsMixin, rest_framework.serializers.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: rest_framework_json_api.serializers.IncludedResourcesValidationMixin, rest_framework_json_api.serializers.SparseFieldsetsMixin, rest_framework.serializers.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 rest_framework_json_api.relations.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

to_representation(instance)

Object instance -> Dict of primitive datatypes.

class rest_framework_json_api.serializers.PolymorphicSerializerMetaclass

Bases: rest_framework.serializers.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: rest_framework_json_api.serializers.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.