Fields#

Contains helper classes for serializing data into JSON format for file storage.

class fma_django_api.v1.fields.JsonToInternalFileField(*args, **kwargs)#

Bases: FileField

Serializes model weights data into JSON.

When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.

to_internal_value(data)#

Converts data to a serialized set.

Parameters:

data (Union[List, numpy.ndarray]) – the model weights

Returns:

a set containing the serialized data

Return type:

Set

bind(field_name, parent)#

Initializes the field name and parent for the field instance. Called when a field is added to the parent serializer instance.

property context#

Returns the context as passed to the root serializer on initialization.

default_empty_html#

alias of empty

default_error_messages = {'empty': 'The submitted file is empty.', 'invalid': 'The submitted data was not a file. Check the encoding type on the form.', 'max_length': 'Ensure this filename has at most {max_length} characters (it has {length}).', 'no_name': 'No filename could be determined.', 'required': 'No file was submitted.'}#
default_validators = []#
fail(key, **kwargs)#

A helper method that simply raises a validation error.

get_attribute(instance)#

Given the outgoing object instance, return the primitive value that should be used for this field.

get_default()#

Return the default value to use when validating data if no input is provided for this field.

If a default has not been set for this field then this will simply raise SkipField, indicating that no value should be set in the validated data for this field.

get_initial()#

Return a value to use when the field is being returned as a primitive value, without any object instance.

get_validators()#
get_value(dictionary)#

Given the incoming primitive data, return the value for this field that should be validated and transformed to a native value.

initial = None#
property root#

Returns the top-level serializer for this field.

run_validation(data=<class 'rest_framework.fields.empty'>)#

Validate a simple representation and return the internal value.

The provided data may be empty if no representation was included in the input.

May raise SkipField if the field should not be included in the validated data.

run_validators(value)#

Test the given value against all the validators on the field, and either raise a ValidationError or simply return.

to_representation(value)#

Transform the outgoing native value into primitive data.

validate_empty_values(data)#

Validate empty values, and either:

  • Raise ValidationError, indicating invalid data.

  • Raise SkipField, indicating that the field should be ignored.

  • 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.

property validators#
class fma_django_api.v1.fields.JsonFileField(*args, **kwargs)#

Bases: FileField

Reads a JSON file as a dictionary.

When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.

to_representation(value)#

Reads a JSON file as a dictionary.

Parameters:

value (Any) – a file containing data about the model

Returns:

a dictionary object containing the contents of the given file

Return type:

Optional[Dict]

bind(field_name, parent)#

Initializes the field name and parent for the field instance. Called when a field is added to the parent serializer instance.

property context#

Returns the context as passed to the root serializer on initialization.

default_empty_html#

alias of empty

default_error_messages = {'empty': 'The submitted file is empty.', 'invalid': 'The submitted data was not a file. Check the encoding type on the form.', 'max_length': 'Ensure this filename has at most {max_length} characters (it has {length}).', 'no_name': 'No filename could be determined.', 'required': 'No file was submitted.'}#
default_validators = []#
fail(key, **kwargs)#

A helper method that simply raises a validation error.

get_attribute(instance)#

Given the outgoing object instance, return the primitive value that should be used for this field.

get_default()#

Return the default value to use when validating data if no input is provided for this field.

If a default has not been set for this field then this will simply raise SkipField, indicating that no value should be set in the validated data for this field.

get_initial()#

Return a value to use when the field is being returned as a primitive value, without any object instance.

get_validators()#
get_value(dictionary)#

Given the incoming primitive data, return the value for this field that should be validated and transformed to a native value.

initial = None#
property root#

Returns the top-level serializer for this field.

run_validation(data=<class 'rest_framework.fields.empty'>)#

Validate a simple representation and return the internal value.

The provided data may be empty if no representation was included in the input.

May raise SkipField if the field should not be included in the validated data.

run_validators(value)#

Test the given value against all the validators on the field, and either raise a ValidationError or simply return.

to_internal_value(data)#

Transform the incoming primitive data into a native value.

validate_empty_values(data)#

Validate empty values, and either:

  • Raise ValidationError, indicating invalid data.

  • Raise SkipField, indicating that the field should be ignored.

  • 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.

property validators#