4. Fields

Each field in a Serializer/Deserializer class is responsible for validating and cleaning the data. You can add additional validators to the field class to further enhanse the field validation.

Note

The serializer fields are defined in fields.py. You can import them using from pyserializer import fields, and refer to the fields as fields.<FieldName>.

4.1. Currently supported serializer fields

4.1.1. CharField:

A text representation. Signature: CharField(source=None, label=None, help_text=None, validators=None)

source (Default: None)
The source name for the field. You can use dot syntax to specify nested source. Eg: version.name.
label (Default: None)
The label for the field.
help_text (Default: None)
The readable help text for the field.
validators (Default: None)

List of validators that should be ran when deserializing the field.

Note

The validators are defined in validators.py. You can import them using from pyserializer import validators, and refer to the validators as validators.<ValidatorName>().

Some commonly used validators with CharField are:

  • RequiredValidator: Signature: RequiredValidator(). Raise a ValidationError if the field value is not supplied during deserialization.

  • MaxLengthValidator: Signature: MaxLengthValidator(max_length). Raise a ValidationError if the field value exceeds the max lenght character limit during deserialization.

    max_length

    The maximum lenght in integer.

  • MinLengthValidator: Signature: MinLengthValidator(min_length). Raise a ValidationError if the field value does not have atlest min lenght characters during deserialization.

    min_length

    The minimum lenght in integer.

4.1.2. DateField:

A date representation. The default format is %Y-%m-%d. Validates the input to match the specified format. Signature: DateField(source=None, label=None, help_text=None, validators=None, format='%Y-%m-%d')

format (Default: ‘%Y-%m-%d’)
A string representing the input or output format.

4.1.3. DateTimeField:

A datetime representation. The default format is 'iso-8601'. Validates the input to match the specified format. Signature: DateTimeField(source=None, label=None, help_text=None, validators=None, format='iso-8601')

format (Default: ‘iso-8601’)
A string representing the input or output format.

4.1.4. UUIDField:

A field that ensures the input is a valid UUID string. Signature: UUIDField(source=None, label=None, help_text=None, validators=None)

4.1.5. NumberField:

An float representation. Signature: NumberField(source=None, label=None, help_text=None, validators=None)

validators (Default: None)

List of validators that should be ran when deserializing the field. Some commonly used validators with NumberField are:

  • RequiredValidator: Signature: RequiredValidator(). Raise a ValidationError if the field value is not supplied during deserialization.

  • MaxValueValidator: Signature: MaxValueValidator(max_value). Raise a ValidationError if the field value exceeds the max value during deserialization.

    max_value

    The maximum value in integer.

  • MinValueValidator: Signature: MinValueValidator(min_value). Raise a ValidationError if the field value is less than the min value during deserialization.

    min_value

    The minimum value in integer.

4.1.6. IntegerField:

An integer representation. Signature: IntegerField(source=None, label=None, help_text=None, validators=None)

validators (Default: None)
List of validators that should be ran when deserializing the field. Some commonly used validators with IntegerField are are inline with NumberField validators. See NumberField:.

4.1.7. FloatField:

An integer representation. Signature: FloatField(source=None, label=None, help_text=None, validators=None)

validators (Default: None)
List of validators that should be ran when deserializing the field. Some commonly used validators with IntegerField are are inline with NumberField validators. See NumberField:.

4.1.8. DictField:

An python dictionary representation. Signature: DictField(source=None, label=None, help_text=None, validators=None)

4.1.9. BooleanField:

An boolean representation. Signature: BooleanField(source=None, label=None, help_text=None, validators=None)

4.1.10. RawField:

A field that does not perform any valudation. Signature: RawField(source=None, label=None, help_text=None, validators=None)

4.1.11. UrlField:

A field that validates the input against a URL matching pattern. Signature: UrlField(source=None, label=None, help_text=None, validators=None)

4.1.12. EmailField:

A field that validates the input to be a valid e-mail address. Signature: EmailField(source=None, label=None, help_text=None, validators=None)

4.1.13. ChoiceField:

A field that validates the input to be within the choice iterable. Signature: ChoiceField(choices, source=None, label=None, help_text=None, validators=None)

choices
A sequence of valid values. eg: (('enabled', 'Enabled'), ('disabled', 'Disabled'))

4.1.14. MethodField:

This that gets its value by calling a method on the serializer class it is attached to. It can be used to add any sort of data to the serialized representation of your object. Signature: MethodField(method_name=None, source=None, label=None, help_text=None, validators=None)

method_name (Default: None)
The name of the serialize method defined in serializer.

See API Reference for complete documentation on the fields.

4.1.15. EnumField:

A field that serializes/deserializes python Enum instances. Signature: EnumField(enum, source=None, label=None, help_text=None, validators=None)

enum
A python enum.Enum class.