mission_control.serializers

Mission Control serializers.

Classes

BlockDiagram(\*args, \*\*kwargs) Attributes to describe a single block diagram.
BlockDiagramSerializer([instance, data]) Block diagram model serializer.
Rover(\*args, \*\*kwargs) Attributes to describe a single rover.
RoverSerializer([instance, data]) Rover model serializer.
class mission_control.serializers.BlockDiagramSerializer(instance=None, data=<class rest_framework.fields.empty>, **kwargs)

Bases: rest_framework.serializers.ModelSerializer

Block diagram model serializer.

class Meta

Meta class.

model

alias of BlockDiagram

BlockDiagramSerializer._get_model_fields(field_names, declared_fields, extra_kwargs)

Returns all the model fields that are being mapped to by fields on the serializer class. Returned as a dict of ‘model field name’ -> ‘model field’. Used internally by get_uniqueness_field_options.

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

BlockDiagramSerializer.build_field(field_name, info, model_class, nested_depth)

Return a two tuple of (cls, kwargs) to build a serializer field with.

BlockDiagramSerializer.build_nested_field(field_name, relation_info, nested_depth)

Create nested fields for forward and reverse relationships.

BlockDiagramSerializer.build_property_field(field_name, model_class)

Create a read only field for model methods and properties.

BlockDiagramSerializer.build_relational_field(field_name, relation_info)

Create fields for forward and reverse relationships.

BlockDiagramSerializer.build_standard_field(field_name, model_field)

Create regular model fields.

BlockDiagramSerializer.build_unknown_field(field_name, model_class)

Raise an error on any unknown fields.

BlockDiagramSerializer.build_url_field(field_name, model_class)

Create a field representing the object’s own URL.

BlockDiagramSerializer.context

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

BlockDiagramSerializer.create(validated_data)

We have a bit of extra checking around this in order to provide descriptive messages when something goes wrong, but this method is essentially just:

return ExampleModel.objects.create(**validated_data)

If there are many to many fields present on the instance then they cannot be set until the model is instantiated, in which case the implementation is like so:

example_relationship = validated_data.pop(‘example_relationship’) instance = ExampleModel.objects.create(**validated_data) instance.example_relationship = example_relationship return instance

The default implementation also does not handle nested relationships. If you want to support writable nested relationships you’ll need to write an explicit .create() method.

BlockDiagramSerializer.default_empty_html

alias of empty

BlockDiagramSerializer.fail(key, **kwargs)

A helper method that simply raises a validation error.

BlockDiagramSerializer.fields

A dictionary of {field_name: field_instance}.

BlockDiagramSerializer.get_attribute(instance)

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

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

BlockDiagramSerializer.get_default_field_names(declared_fields, model_info)

Return the default list of field names that will be used if the Meta.fields option is not specified.

BlockDiagramSerializer.get_extra_kwargs()

Return a dictionary mapping field names to a dictionary of additional keyword arguments.

BlockDiagramSerializer.get_field_names(declared_fields, info)

Returns the list of all field names that should be created when instantiating this serializer class. This is based on the default set of fields, but also takes into account the Meta.fields or Meta.exclude options if they have been specified.

BlockDiagramSerializer.get_fields()

Return the dict of field names -> field instances that should be used for self.fields when instantiating the serializer.

BlockDiagramSerializer.get_unique_for_date_validators()

Determine a default set of validators for the following constraints:

  • unique_for_date
  • unique_for_month
  • unique_for_year
BlockDiagramSerializer.get_unique_together_validators()

Determine a default set of validators for any unique_together constraints.

BlockDiagramSerializer.get_uniqueness_extra_kwargs(field_names, declared_fields, extra_kwargs)

Return any additional field options that need to be included as a result of uniqueness constraints on the model. This is returned as a two-tuple of:

(‘dict of updated extra kwargs’, ‘mapping of hidden fields’)

BlockDiagramSerializer.get_validators()

Determine the set of validators to use when instantiating serializer.

BlockDiagramSerializer.include_extra_kwargs(kwargs, extra_kwargs)

Include any ‘extra_kwargs’ that have been included for this field, possibly removing any incompatible existing keyword arguments.

BlockDiagramSerializer.many_init(*args, **kwargs)

This method implements the creation of a ListSerializer parent class when many=True is used. You can customize it if you need to control which keyword arguments are passed to the parent, and which are passed to the child.

Note that we’re over-cautious in passing most arguments to both parent and child classes in order to try to cover the general case. If you’re overriding this method you’ll probably want something much simpler, eg:

@classmethod def many_init(cls, *args, **kwargs):

kwargs[‘child’] = cls() return CustomListSerializer(*args, **kwargs)
BlockDiagramSerializer.root

Returns the top-level serializer for this field.

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

We override the default run_validation, because the validation performed by validators and the .validate() method should be coerced into an error dictionary with a ‘non_fields_error’ key.

BlockDiagramSerializer.run_validators(value)

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

alias of SlugRelatedField

BlockDiagramSerializer.serializer_url_field

alias of HyperlinkedIdentityField

BlockDiagramSerializer.to_internal_value(data)

Dict of native values <- Dict of primitive datatypes.

BlockDiagramSerializer.to_representation(instance)

Object instance -> Dict of primitive datatypes.

BlockDiagramSerializer.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.
class mission_control.serializers.RoverSerializer(instance=None, data=<class rest_framework.fields.empty>, **kwargs)

Bases: rest_framework.serializers.ModelSerializer

Rover model serializer.

class Meta

Meta class.

model

alias of Rover

RoverSerializer._get_model_fields(field_names, declared_fields, extra_kwargs)

Returns all the model fields that are being mapped to by fields on the serializer class. Returned as a dict of ‘model field name’ -> ‘model field’. Used internally by get_uniqueness_field_options.

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

RoverSerializer.build_field(field_name, info, model_class, nested_depth)

Return a two tuple of (cls, kwargs) to build a serializer field with.

RoverSerializer.build_nested_field(field_name, relation_info, nested_depth)

Create nested fields for forward and reverse relationships.

RoverSerializer.build_property_field(field_name, model_class)

Create a read only field for model methods and properties.

RoverSerializer.build_relational_field(field_name, relation_info)

Create fields for forward and reverse relationships.

RoverSerializer.build_standard_field(field_name, model_field)

Create regular model fields.

RoverSerializer.build_unknown_field(field_name, model_class)

Raise an error on any unknown fields.

RoverSerializer.build_url_field(field_name, model_class)

Create a field representing the object’s own URL.

RoverSerializer.context

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

RoverSerializer.create(validated_data)

We have a bit of extra checking around this in order to provide descriptive messages when something goes wrong, but this method is essentially just:

return ExampleModel.objects.create(**validated_data)

If there are many to many fields present on the instance then they cannot be set until the model is instantiated, in which case the implementation is like so:

example_relationship = validated_data.pop(‘example_relationship’) instance = ExampleModel.objects.create(**validated_data) instance.example_relationship = example_relationship return instance

The default implementation also does not handle nested relationships. If you want to support writable nested relationships you’ll need to write an explicit .create() method.

RoverSerializer.default_empty_html

alias of empty

RoverSerializer.fail(key, **kwargs)

A helper method that simply raises a validation error.

RoverSerializer.fields

A dictionary of {field_name: field_instance}.

RoverSerializer.get_attribute(instance)

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

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

RoverSerializer.get_default_field_names(declared_fields, model_info)

Return the default list of field names that will be used if the Meta.fields option is not specified.

RoverSerializer.get_extra_kwargs()

Return a dictionary mapping field names to a dictionary of additional keyword arguments.

RoverSerializer.get_field_names(declared_fields, info)

Returns the list of all field names that should be created when instantiating this serializer class. This is based on the default set of fields, but also takes into account the Meta.fields or Meta.exclude options if they have been specified.

RoverSerializer.get_fields()

Return the dict of field names -> field instances that should be used for self.fields when instantiating the serializer.

RoverSerializer.get_unique_for_date_validators()

Determine a default set of validators for the following constraints:

  • unique_for_date
  • unique_for_month
  • unique_for_year
RoverSerializer.get_unique_together_validators()

Determine a default set of validators for any unique_together constraints.

RoverSerializer.get_uniqueness_extra_kwargs(field_names, declared_fields, extra_kwargs)

Return any additional field options that need to be included as a result of uniqueness constraints on the model. This is returned as a two-tuple of:

(‘dict of updated extra kwargs’, ‘mapping of hidden fields’)

RoverSerializer.get_validators()

Determine the set of validators to use when instantiating serializer.

RoverSerializer.include_extra_kwargs(kwargs, extra_kwargs)

Include any ‘extra_kwargs’ that have been included for this field, possibly removing any incompatible existing keyword arguments.

RoverSerializer.many_init(*args, **kwargs)

This method implements the creation of a ListSerializer parent class when many=True is used. You can customize it if you need to control which keyword arguments are passed to the parent, and which are passed to the child.

Note that we’re over-cautious in passing most arguments to both parent and child classes in order to try to cover the general case. If you’re overriding this method you’ll probably want something much simpler, eg:

@classmethod def many_init(cls, *args, **kwargs):

kwargs[‘child’] = cls() return CustomListSerializer(*args, **kwargs)
RoverSerializer.root

Returns the top-level serializer for this field.

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

We override the default run_validation, because the validation performed by validators and the .validate() method should be coerced into an error dictionary with a ‘non_fields_error’ key.

RoverSerializer.run_validators(value)

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

alias of SlugRelatedField

RoverSerializer.serializer_url_field

alias of HyperlinkedIdentityField

RoverSerializer.to_internal_value(data)

Dict of native values <- Dict of primitive datatypes.

RoverSerializer.to_representation(instance)

Object instance -> Dict of primitive datatypes.

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