8. API Reference¶
8.1. Serializer¶
-
class
pyserializer.serializers.BaseSerializer(instance=None, data_dict=None, source=None, many=False, allow_blank_source=False, *args, **kwargs)¶ This is the Serializer implementation.
Parameters: - instance – Python object which has to be serialized.
- data_dict – Dictionary object which has to be deserialized.
- source – The source name.
- many – A Bool field which should be set True when the instance argument is a list(Serializing multiple python objects in a list). The Default is False
- allow_blank_source – A Bool field which should be set True if the serializer should not throw an error when the source defined on the field is not present on the instance. The default is False
-
data¶ Returns the serialized data on the serializer. Caches the data once created. Uses the cached version next time when the data property is accessed.
-
errors¶ Runs the deserialization and returns any validations errors. Also, sets the object property if no errors occurred during validation.
-
get_fields()¶ Returns the complete set of fields defined in the Serializer class as a dict.
-
invoke_validators_and_set_errors(field_name, validators, value)¶ Calls the validators on the fields. Catches the ValidationError raised and stores the validation error message to self._errors object.
-
metadata()¶ Return a dictionary of metadata about the fields on the serializer.
-
object¶ The deserialized object. Runs the validators and checks the error object before deserializing the object . Caches the object once created. Uses the cached version next time when the object property is accessed.
-
perform_validation(fields, data)¶ Runs the validators specified on the fields and sets the error messages.
-
restore_field(field_name, field, data)¶ Given a field and a deserializable data dictionary, fetches the relevent field data from the data dictionary and returns the deserialized version of the field data along with the field name. Calls the to_python method on each field.
Parameters: - field_name – The field name.
- field – The field object.
- data – The data dictionary which contains restored field objects.
-
restore_fields(data)¶ Converts a dictionary of data into a dictionary of deserialized fields.
Parameters: data – The data dictionary passed in to be deserialized.
-
restore_object(instance=None)¶ Deserialize a dictionary of attributes into an object instance.
Parameters: instance – The isntance on which the deserialized object should be set.
-
set_field_value_on_instance(instance, field_name, field, data)¶ Fetches the field value from data and, sets the field name and value of the field on the instance.
-
to_native(obj)¶ Serializes objects. Calls the field_to_native method on each fields.
Parameters: obj – The python object passed in to be serialized.
8.2. Fields¶
-
class
pyserializer.fields.Field(source=None, label=None, help_text=None, validators=None, error_messages=None, *args, **kwargs)¶ A base class for fields in a Serializer.
Parameters: - source – (str) The source name for the field. You can use dot syntax to specify nested source. Eg: ‘version.name’
- label – (optional) The label for the field.
- help_text – (optional) The readable help text for the field.
- validators – List of validators that should be ran when deserializing the field. This list will be appended along with the default_validators defined on each field.
- error_messages – (dict) Custom error message on the field.
-
class
pyserializer.fields.CharField(*args, **kwargs)¶ A string field.
Parameters: - args – Arguments passed directly into the parent
Field. - kwargs – Keyword arguments passed directly into the parent
Field.
- args – Arguments passed directly into the parent
-
class
pyserializer.fields.DateField(format=None, *args, **kwargs)¶ A date field.
Parameters: - format – The format of the date. Defaults to %Y-%m-%d.
- args – Arguments passed directly into the parent
Field. - kwargs – Keyword arguments passed directly into the parent
Field.
-
class
pyserializer.fields.DateTimeField(format=None, *args, **kwargs)¶ A datetime field.
Parameters: - format – The format of the datetime. Defaults to ISO_8601.
- args – Arguments passed directly into the parent
Field. - kwargs – Keyword arguments passed directly into the parent
Field.
-
class
pyserializer.fields.UUIDField(*args, **kwargs)¶ A UUID4 field.
Parameters: - args – Arguments passed directly into the parent
Field. - kwargs – Keyword arguments passed directly into the parent
Field.
- args – Arguments passed directly into the parent
-
class
pyserializer.fields.NumberField(*args, **kwargs)¶ A number field. The default num_type is float.
Parameters: - args – Arguments passed directly into the parent
Field. - kwargs – Keyword arguments passed directly into the parent
Field.
- args – Arguments passed directly into the parent
-
class
pyserializer.fields.IntegerField(*args, **kwargs)¶ A integer field.
Parameters: - args – Arguments passed directly into the parent
Field. - kwargs – Keyword arguments passed directly into the parent
Field.
- args – Arguments passed directly into the parent
-
class
pyserializer.fields.FloatField(*args, **kwargs)¶ A float field.
Parameters: - args – Arguments passed directly into the parent
Field. - kwargs – Keyword arguments passed directly into the parent
Field.
- args – Arguments passed directly into the parent
-
class
pyserializer.fields.DictField(source=None, label=None, help_text=None, validators=None, error_messages=None, *args, **kwargs)¶ A dict field.
Parameters: - source – (str) The source name for the field. You can use dot syntax to specify nested source. Eg: ‘version.name’
- label – (optional) The label for the field.
- help_text – (optional) The readable help text for the field.
- validators – List of validators that should be ran when deserializing the field. This list will be appended along with the default_validators defined on each field.
- error_messages – (dict) Custom error message on the field.
-
class
pyserializer.fields.RawField(source=None, label=None, help_text=None, validators=None, error_messages=None, *args, **kwargs)¶ A raw field. Field that does not apply any validation
Parameters: - source – (str) The source name for the field. You can use dot syntax to specify nested source. Eg: ‘version.name’
- label – (optional) The label for the field.
- help_text – (optional) The readable help text for the field.
- validators – List of validators that should be ran when deserializing the field. This list will be appended along with the default_validators defined on each field.
- error_messages – (dict) Custom error message on the field.
-
class
pyserializer.fields.UrlField(source=None, label=None, help_text=None, validators=None, error_messages=None, *args, **kwargs)¶ A url field.
Parameters: - source – (str) The source name for the field. You can use dot syntax to specify nested source. Eg: ‘version.name’
- label – (optional) The label for the field.
- help_text – (optional) The readable help text for the field.
- validators – List of validators that should be ran when deserializing the field. This list will be appended along with the default_validators defined on each field.
- error_messages – (dict) Custom error message on the field.
-
class
pyserializer.fields.MethodField(method_name=None, *args, **kwargs)¶ A method field.
Parameters: - method_name – The name of the serialize method defined in serializer.
- args – Arguments passed directly into the parent
Field. - kwargs – Keyword arguments passed directly into the parent
Field.
-
class
pyserializer.fields.EnumField(enum, *args, **kwargs)¶ A field that serializes/deserializes python Enum instances.
Parameters: - enum – An
enum.Enumclass. - args – Arguments passed directly into the parent
Field. - kwargs – Keyword arguments passed directly into the parent
Field.
- enum – An
8.3. Validators¶
-
class
pyserializer.validators.BaseValidator(*args, **kwargs)¶ A base class for validators.
-
class
pyserializer.validators.RequiredValidator(*args, **kwargs)¶ A required field validator.
-
class
pyserializer.validators.MaxValueValidator(max_value, *args, **kwargs)¶ A max value validator.
Parameters: max_value – (required) A maximum value in integer. This will ensure that the value passed in to this validator is less than or equal to max_value.
-
class
pyserializer.validators.MinValueValidator(min_value, *args, **kwargs)¶ A min value validator.
Parameters: min_value – (required) A minimum value in integer. This will ensure that the value passed in to this validator is greater than or equal to min_value.
-
class
pyserializer.validators.MaxLengthValidator(max_length, *args, **kwargs)¶ A mix length validator.
Parameters: max_length – (required) A maximum length value in integer. This will ensure that the value passed in to this validator will have atmost max_length characters.
-
class
pyserializer.validators.MinLengthValidator(min_length, *args, **kwargs)¶ A min length validator.
Parameters: min_length – (required) A minimum length value in integer. This will ensure that the value passed in to this validator will have atleast min_length characters.
-
class
pyserializer.validators.EmailValidator(domain_blacklist=None, *args, **kwargs)¶ A email validator.
Parameters: domain_blacklist – (optional) A list of domains which should be considered as invalid. Defaults to an empty list.
-
class
pyserializer.validators.NumberValidator(*args, **kwargs)¶ A number validator. The default num_type is float
-
class
pyserializer.validators.IntegerValidator(*args, **kwargs)¶ A integer validator.
-
class
pyserializer.validators.FloatValidator(*args, **kwargs)¶ A float validator.
-
class
pyserializer.validators.DecimalValidator(*args, **kwargs)¶ A decimal validator.
-
class
pyserializer.validators.DictValidator(*args, **kwargs)¶ A dict validator.
-
class
pyserializer.validators.UUIDValidator(*args, **kwargs)¶ A UUID validator.
-
class
pyserializer.validators.DateTimeValidator(format=None, *args, **kwargs)¶ A DateTime validator.
Parameters: format – (optional) The format of the datetime. Defaults to ISO_8601.
-
class
pyserializer.validators.DateValidator(format=None, *args, **kwargs)¶ A Date validator.
Parameters: format – (optional) The format of the datetime. Defaults to ISO_8601.
-
class
pyserializer.validators.BooleanValidator(*args, **kwargs)¶ A Boolean validator.
-
class
pyserializer.validators.UrlValidator(allowable_schemes=None, url_regex=None, *args, **kwargs)¶ A URL validator.
Parameters: - allowable_schemes – (optional) A set of allowable schemes in url. Uses the default_schemes if not specified.
- url_regex – (optional) A url regex for validating the value. Uses the URL_REGEX if not specified.
-
class
pyserializer.validators.MethodValidator(*args, **kwargs)¶ A Method validator. Checks to see if the method is callable.