API Reference

Serializer

class drf_turbo.BaseSerializer(instance=None, bool many=False, data=None, dict context=None, only=None, exclude=None, bool partial=False, **kwargs)

Base class for all serializers.

Parameters
  • instance – The instance to be serialized.

  • many – Serialize single object or a list of objects.

  • data – The data to be deserialized.

  • context – The context dictionary passed to the serializer.

  • partial – If set to True, partial update will be allowed.

  • only – A list of fields to be included in the serialized data.

  • exclude – A list of fields to be excluded from the serialized data.

  • kwargs – Extra keyword arguments.

context

context: dict

data

Return the serialized data on the serializer.

errors

Return the dictionary of errors raised during validation.

get_initial_data(self) dict

Return the initial data for the fields.

instance

Return the model instance that is being serialized.

is_valid(self, bool raise_exception=False) bool

Whether the data is valid.

Parameters

raise_exception – Whether to raise an exception if the data is invalid.

many

many: ‘bool’

partial

partial: ‘bool’

save(self, **kwargs)

Create or update a model instance.

Parameters

kwargs – Extra keyword arguments.

validated_data

Return the validated data on the serializer.

class drf_turbo.Serializer

Bases: drf_turbo.serializer.BaseSerializer

bind()

Update the field name and root for the field instance.

Field_name

The name of the field.

Root

The root of the field.

context

context: dict

data

Return the serialized data on the serializer.

deserialize(self, data, dict context)

Given a dictionary-like structure, build a dictionary of deserialized fields and return a model instance.

Parameters
  • data – The data to deserialize.

  • context – The context for the request.

errors

Return the dictionary of errors raised during validation.

fields

This is a shortcut for accessing the dict on the fields attribute.

get_attribute()

Return the value of the field from the provided instance.

get_default_value()

Return the default value for this field.

get_fields(self)

Return the dict of field names -> field instances that should be added to the serializer.

get_initial()

Return the initial value for this field.

get_initial_data(self) dict

Return the initial data for the fields.

instance

Return the model instance that is being serialized.

is_valid(self, bool raise_exception=False) bool

Whether the data is valid.

Parameters

raise_exception – Whether to raise an exception if the data is invalid.

many

many: ‘bool’

method_getter()

Returns a function that fetches an attribute from an object.

Field_name

The name of the attribute to get.

Root

The root of the field.

partial

partial: ‘bool’

raise_if_fail()

Helper method to make a ValidationError with an error message from self.error_messages.

run_validation(self, data, dict context)

Validate an entire bundle of data.

Parameters
  • data – The data to validate.

  • context – The context for the request.

save(self, **kwargs)

Create or update a model instance.

Parameters

kwargs – Extra keyword arguments.

serialize(self, instance, dict context)

Serialize a model instance.

Parameters
  • instance – Model instance to serialize.

  • context – Context data.

validate(self, data)

Validate a dictionary of deserialized field values.

Parameters

data – A dictionary of deserialized field values.

validate_empty_values()

Validate empty values, and either: * Raise ValidationError, indicating invalid data. * Return (True, data), indicating an empty value that should be

returned without any further validation being applied.

  • Return (False, data), indicating a non-empty value, that should have validation applied as normal.

validate_or_raise()

Validate the value and raise a ValidationError if validation fails.

validated_data

Return the validated data on the serializer.

class drf_turbo.ModelSerializer

Bases: drf_turbo.meta.Serializer

bind()

Update the field name and root for the field instance.

Field_name

The name of the field.

Root

The root of the field.

context

context: dict

create(self, validated_data)

Create a model instance.

Parameters

validated_data – A dictionary of validated data.

data

Return the serialized data on the serializer.

deserialize(self, data, dict context)

Given a dictionary-like structure, build a dictionary of deserialized fields and return a model instance.

Parameters
  • data – The data to deserialize.

  • context – The context for the request.

errors

Return the dictionary of errors raised during validation.

fields

This is a shortcut for accessing the dict on the fields attribute.

get_attribute()

Return the value of the field from the provided instance.

get_default_value()

Return the default value for this field.

get_fields(self)

Return the dict of field names -> field instances that should be added to the serializer.

get_initial()

Return the initial value for this field.

get_initial_data(self) dict

Return the initial data for the fields.

instance

Return the model instance that is being serialized.

is_valid(self, bool raise_exception=False) bool

Whether the data is valid.

Parameters

raise_exception – Whether to raise an exception if the data is invalid.

many

many: ‘bool’

method_getter()

Returns a function that fetches an attribute from an object.

Field_name

The name of the attribute to get.

Root

The root of the field.

partial

partial: ‘bool’

raise_if_fail()

Helper method to make a ValidationError with an error message from self.error_messages.

run_validation(self, data, dict context)

Validate an entire bundle of data.

Parameters
  • data – The data to validate.

  • context – The context for the request.

save(self, **kwargs)

Create or update a model instance.

Parameters

kwargs – Extra keyword arguments.

serialize(self, instance, dict context)

Serialize a model instance.

Parameters
  • instance – Model instance to serialize.

  • context – Context data.

update(self, instance, validated_data)

Update a model instance.

Parameters
  • instance – Model instance to update.

  • validated_data – A dictionary of deserialized data.

validate(self, data)

Validate a dictionary of deserialized field values.

Parameters

data – A dictionary of deserialized field values.

validate_empty_values()

Validate empty values, and either: * Raise ValidationError, indicating invalid data. * Return (True, data), indicating an empty value that should be

returned without any further validation being applied.

  • Return (False, data), indicating a non-empty value, that should have validation applied as normal.

validate_or_raise()

Validate the value and raise a ValidationError if validation fails.

validated_data

Return the validated data on the serializer.

Fields

class drf_turbo.Field

Basic field from which other fields should extend. It applies no formatting by default, and should only be used in cases where data does not need to be formatted before being serialized or deserialized. On error, the name of the field will be returned.

Parameters
  • attr (str) – The name of the attribute to get the value from when serializing.

  • call (bool) – Whether the value should be called after it is retrieved from the object. Useful if an object has a method to be serialized.

  • required (bool) – Raise a ValidationError if the field value is not supplied during deserialization.

  • write_only (bool) – If True skip this field during serialization, otherwise its value will be present in the serialized data.

  • read_only (bool) – If True skip this field during deserialization, otherwise its value will be present in the deserialized object.

  • label (str) – A label to use as the name of the serialized field instead of using the attribute name of the field.

  • validators (list) – A list of validators to apply against incoming data during deserialization.

  • field_name (str) – The name of field.

  • root (str) – The root(parent) of field.

  • default_value – Default value to be used during serialization and deserialization.

  • initial – The initial value for the field.

bind()

Update the field name and root for the field instance.

Field_name

The name of the field.

Root

The root of the field.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

get_attribute()

Return the value of the field from the provided instance.

get_default_value()

Return the default value for this field.

get_initial()

Return the initial value for this field.

method_getter()

Returns a function that fetches an attribute from an object.

Field_name

The name of the attribute to get.

Root

The root of the field.

raise_if_fail()

Helper method to make a ValidationError with an error message from self.error_messages.

run_validation()

Validate an input data.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

validate_empty_values()

Validate empty values, and either: * Raise ValidationError, indicating invalid data. * Return (True, data), indicating an empty value that should be

returned without any further validation being applied.

  • Return (False, data), indicating a non-empty value, that should have validation applied as normal.

validate_or_raise()

Validate the value and raise a ValidationError if validation fails.

class drf_turbo.StrField

” A field that validates input as an string.

Parameters

kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.EmailField

A field that validates input as an E-Mail address.

Parameters
  • to_lower – If True, convert the value to lowercase before validating.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.URLField

A field that validates input as an URL.

Parameters

kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

class drf_turbo.RegexField

A field that validates input against a given regular expression.

Parameters
  • regex – The regular expression to use for validation.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

class drf_turbo.IPField

A field that validates that input is an IP address.

Parameters

kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

class drf_turbo.UUIDField

A field that validates input as an UUID.

Parameters

kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

class drf_turbo.PasswordField

A field that validates input as a password.

Parameters

kwargs – The same keyword arguments that Field receives.

class drf_turbo.SlugField

Slug field type.

Parameters
  • allow_unicode – If True, allow unicode characters in the field.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

class drf_turbo.IntField

A field that validates input as an integer.

Parameters

min_value – The minimum value allowed.

:param max_value : The maximum value allowed. :param kwargs: The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.FloatField

A field that validates input as a float.

Parameters
  • min_value – The minimum value allowed.

  • max_value – The maximum value allowed.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.DecimalField

A field that validates input as a Python Decimal.

Parameters
  • max_digits(required) – Maximum number of digits.

  • deciaml_places(required) – Number of decimal places.

  • min_value – The minimum value allowed.

  • max_value – The maximum value allowed.

  • coerce_to_string – If True, values will be converted to strings during serialization.

  • rounding – How to round the value during serialization.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Validate that the input is a decimal number and return a Decimal instance.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.BoolField

Boolean field type.

Parameters

kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.ChoiceField

Choice field type.

Parameters
  • choices(required) – A list of valid choices.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.MultipleChoiceField

Multiple choice field type.

Parameters
  • allow_empty – If True, allow the user to leave the field blank.

  • kwargs – The same keyword arguments that Field and class:ChoiceField receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.DateTimeField

A field that (de)serializes to a datetime.datetime object.

Parameters
  • format – The format to use when serializing/deserializing.

  • input_formats – A list of formats to check for when deserializing input.

  • default_timezone – The timezone to use when creating datetime instances.

  • kwargs – The same keyword arguments that Field receives.

datetime_parser()

string, format -> new datetime parsed from a string (like time.strptime()).

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

enforce_timezone()

When self.default_timezone is None, always return naive datetimes. When self.default_timezone is not None, always return aware datetimes.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.DateField

A field that (de)serializes to a datetime.date object.

Parameters
  • format – Either "%Y-%m-%d" or "%m/%d/%Y", or a custom string of the format passed to strftime.

  • input_formats – A list of strings, or a custom list of input formats to be used to parse the input date string.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.TimeField

A field that (de)serializes to a datetime.time object.

Parameters
  • format – Either “time” or “datetime” for serialization to display time in either 24 hour or 12 hour+minute+second format.

  • input_formats – Optional list of formats to also be accepted.

  • kwargs – The same keyword arguments that Field receives.

datetime_parser()

string, format -> new datetime parsed from a string (like time.strptime()).

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.FileField

A file field.

Parameters
  • max_length – The maximum file size.

  • alow_empty_file – Whether to allow uploading empty files.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.ArrayField

An Array Field.

Parameters
  • child – The field to validate the array elements.

  • allow_empty – Whether the array can be empty.

  • max_items – The maximum number of items allowed in the array.

  • min_items – The minimum number of items required in the array.

  • exac_items – The exact number of items required in the array.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

List of dicts of native values <- List of dicts of primitive datatypes.

serialize()

List of object instances -> List of dicts of primitive datatypes.

class drf_turbo.DictField

A Dict Field.

Parameters
  • child – The field to validate the dict values.

  • allow_empty – Whether the dict can be empty.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

List of dicts of native values <- List of dicts of primitive datatypes.

serialize()

List of object instances -> List of dicts of primitive datatypes.

class drf_turbo.JSONField

A JSON Field.

Parameters
  • binary – Whether to load/dump JSON as binary data.

  • encoder – The JSON encoder class to use.

  • decoder – The JSON decoder class to use.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.RelatedField

A Related Field.

Parameters

queryset – The queryset to use for getting the instance from the given value.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

class drf_turbo.ManyRelatedField

A field used to represent a to-many relationship.

Parameters
  • child_relation – The model field that is the reverse of the relation.

  • allow_empty – Whether the list can be empty.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.ConstantField

A field that always outputs a fixed value.

Parameters
  • constant – The value to return.

  • kwargs – The same keyword arguments that Field receives.

deserialize()

Transform the incoming primitive data into a native value.

Parameters
  • data – The incoming data.

  • context – The context for the request.

class drf_turbo.RecursiveField

A field that recursively validates its data.

Parameters
  • many – Whether the field is a collection of items.

  • context – The context passed to the field’s run_validation().

  • only – A tuple or list of field names to include.

  • exclude – A tuple or list of field names to exclude.

  • kwargs – The same keyword arguments that Field receives.

serialize()

Transform the outgoing native value into primitive data

Parameters
  • value – The outgoing value.

  • context – The context for the request.

class drf_turbo.MethodField

A field that calls a method on the serializer instead of simply returning a value.

Parameters
  • method_name – The name of the method to call.

  • kwargs – The same keyword arguments that Field receives.

method_getter()

Returns a function that fetches an attribute from an object.

Field_name

The name of the attribute to get.

Root

The root of the field.