rest_framework_tricks.serializers package

Submodules

rest_framework_tricks.serializers.nested_proxy module

Serializers.

The following code is used in the usage examples of the ModelSerializer and HyperlinkedModelSerializer classes.

>>> from rest_framework import serializers
>>>
>>> from .models import Book
>>>
>>>
>>> class PublishingInformationSerializer(serializers.ModelSerializer):
>>>
>>>     publication_date = serializers.DateField(required=False)
>>>     isbn = serializers.CharField(required=False)
>>>     pages = serializers.IntegerField(required=False)
>>>
>>>     class Meta:
>>>
>>>         model = Book
>>>         fields = (
>>>             'publication_date',
>>>             'isbn',
>>>             'pages',
>>>         )
>>>         nested_proxy_field = True
>>>
>>>
>>> class StockInformationSerializer(serializers.ModelSerializer):
>>>
>>>     class Meta:
>>>
>>>         model = Book
>>>         fields = (
>>>             'stock_count',
>>>             'price',
>>>             'state',
>>>         )
>>>         nested_proxy_field = True
class rest_framework_tricks.serializers.nested_proxy.HyperlinkedModelSerializer(*args, **kwargs)[source]

Bases: rest_framework.serializers.HyperlinkedModelSerializer

HyperlinkedModelSerializer for models with NestedProxyField fields.

Example:

>>> from rest_framework_tricks.serializers import (
>>>     HyperlinkedModelSerializer,
>>> )
>>>
>>>
>>> class BookSerializer(HyperlinkedModelSerializer):
>>>
>>>     publishing_information = PublishingInformationSerializer(
>>>         required=False
>>>     )
>>>     stock_information = StockInformationSerializer(required=False)
>>>
>>>     class Meta:
>>>
>>>         model = Book
>>>         fields = (
>>>             'url',
>>>             'id',
>>>             'title',
>>>             'description',
>>>             'summary',
>>>             'publishing_information',
>>>             'stock_information',
>>>         )
create(validated_data)[source]

Create.

Parameters

validated_data

Returns

update(instance, validated_data)[source]

Update.

Parameters
  • instance

  • validated_data

Returns

class rest_framework_tricks.serializers.nested_proxy.ModelSerializer(*args, **kwargs)[source]

Bases: rest_framework.serializers.ModelSerializer

ModelSerializer for models with NestedProxyField fields.

Example:

>>> from rest_framework_tricks.serializers import ModelSerializer
>>>
>>>
>>> class BookSerializer(ModelSerializer):
>>>
>>>     publishing_information = PublishingInformationSerializer(
>>>         required=False
>>>     )
>>>     stock_information = StockInformationSerializer(required=False)
>>>
>>>     class Meta:
>>>
>>>         model = Book
>>>         fields = (
>>>             'url',
>>>             'id',
>>>             'title',
>>>             'description',
>>>             'summary',
>>>             'publishing_information',
>>>             'stock_information',
>>>         )
create(validated_data)[source]

Create.

Parameters

validated_data

Returns

update(instance, validated_data)[source]

Update.

Parameters
  • instance

  • validated_data

Returns

class rest_framework_tricks.serializers.nested_proxy.NestedProxyFieldIdentifier[source]

Bases: object

NestedProxyField identifier.

rest_framework_tricks.serializers.nested_proxy.extract_nested_serializers(serializer, validated_data, nested_serializers=None, nested_serializers_data=None)[source]

Extract nested serializers.

Parameters
  • serializer (rest_framework.serializers.Serializer) – Serializer instance.

  • validated_data (dict) – Validated data.

  • nested_serializers (dict) –

  • nested_serializers_data

Returns

Return type

tuple

rest_framework_tricks.serializers.nested_proxy.is_nested_proxy_field(field)[source]

Check if field is nested proxy field.

Parameters

field

Returns

True or False

Return type

bool

rest_framework_tricks.serializers.nested_proxy.set_instance_values(nested_serializers, nested_serializers_data, instance)[source]

Set values on instance.

Does not perform any save actions.

Parameters
  • nested_serializers – Nested serializers.

  • nested_serializers_data – Nested serializers data.

  • instance – Instance (not yet saved)

Returns

Same instance with values set.

Return type

Module contents

Serializers.

class rest_framework_tricks.serializers.HyperlinkedModelSerializer(*args, **kwargs)[source]

Bases: rest_framework.serializers.HyperlinkedModelSerializer

HyperlinkedModelSerializer for models with NestedProxyField fields.

Example:

>>> from rest_framework_tricks.serializers import (
>>>     HyperlinkedModelSerializer,
>>> )
>>>
>>>
>>> class BookSerializer(HyperlinkedModelSerializer):
>>>
>>>     publishing_information = PublishingInformationSerializer(
>>>         required=False
>>>     )
>>>     stock_information = StockInformationSerializer(required=False)
>>>
>>>     class Meta:
>>>
>>>         model = Book
>>>         fields = (
>>>             'url',
>>>             'id',
>>>             'title',
>>>             'description',
>>>             'summary',
>>>             'publishing_information',
>>>             'stock_information',
>>>         )
create(validated_data)[source]

Create.

Parameters

validated_data

Returns

update(instance, validated_data)[source]

Update.

Parameters
  • instance

  • validated_data

Returns

class rest_framework_tricks.serializers.ModelSerializer(*args, **kwargs)[source]

Bases: rest_framework.serializers.ModelSerializer

ModelSerializer for models with NestedProxyField fields.

Example:

>>> from rest_framework_tricks.serializers import ModelSerializer
>>>
>>>
>>> class BookSerializer(ModelSerializer):
>>>
>>>     publishing_information = PublishingInformationSerializer(
>>>         required=False
>>>     )
>>>     stock_information = StockInformationSerializer(required=False)
>>>
>>>     class Meta:
>>>
>>>         model = Book
>>>         fields = (
>>>             'url',
>>>             'id',
>>>             'title',
>>>             'description',
>>>             'summary',
>>>             'publishing_information',
>>>             'stock_information',
>>>         )
create(validated_data)[source]

Create.

Parameters

validated_data

Returns

update(instance, validated_data)[source]

Update.

Parameters
  • instance

  • validated_data

Returns

class rest_framework_tricks.serializers.NestedProxyFieldIdentifier[source]

Bases: object

NestedProxyField identifier.

rest_framework_tricks.serializers.extract_nested_serializers(serializer, validated_data, nested_serializers=None, nested_serializers_data=None)[source]

Extract nested serializers.

Parameters
  • serializer (rest_framework.serializers.Serializer) – Serializer instance.

  • validated_data (dict) – Validated data.

  • nested_serializers (dict) –

  • nested_serializers_data

Returns

Return type

tuple

rest_framework_tricks.serializers.is_nested_proxy_field(field)[source]

Check if field is nested proxy field.

Parameters

field

Returns

True or False

Return type

bool

rest_framework_tricks.serializers.set_instance_values(nested_serializers, nested_serializers_data, instance)[source]

Set values on instance.

Does not perform any save actions.

Parameters
  • nested_serializers – Nested serializers.

  • nested_serializers_data – Nested serializers data.

  • instance – Instance (not yet saved)

Returns

Same instance with values set.

Return type