class documentation

A `ModelSerializer` is just a regular `Serializer`, except that: * A set of default fields are automatically populated. * A set of default validators are automatically populated. * Default `.create()` and `.update()` implementations are provided. The process of automatically determining a set of serializer fields based on the model fields is reasonably complex, but you almost certainly don't need to dig into the implementation. If the `ModelSerializer` class *doesn't* generate the set of fields that you need you should either declare the extra/differing fields explicitly on the serializer class, or simply use a `Serializer` class.

Method build_field Return a two tuple of (cls, kwargs) to build a serializer field with.
Method build_nested_field Create nested fields for forward and reverse relationships.
Method build_property_field Create a read only field for model methods and properties.
Method build_relational_field Create fields for forward and reverse relationships.
Method build_standard_field Create regular model fields.
Method build_unknown_field Raise an error on any unknown fields.
Method build_url_field Create a field representing the object's own URL.
Method create 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:
Method get_default_field_names Return the default list of field names that will be used if the `Meta.fields` option is not specified.
Method get_extra_kwargs Return a dictionary mapping field names to a dictionary of additional keyword arguments.
Method get_field_names 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.
Method get_fields Return the dict of field names -> field instances that should be used for `self.fields` when instantiating the serializer.
Method get_unique_for_date_validators Determine a default set of validators for the following constraints:
Method get_unique_together_validators Determine a default set of validators for any unique_together constraints.
Method get_uniqueness_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:
Method get_validators Determine the set of validators to use when instantiating serializer.
Method include_extra_kwargs Include any 'extra_kwargs' that have been included for this field, possibly removing any incompatible existing keyword arguments.
Method update Undocumented
Class Variable serializer_field_mapping Undocumented
Instance Variable url_field_name Undocumented
Method _get_model_fields 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`.

Inherited from Serializer:

Method __getitem__ Undocumented
Method __iter__ Undocumented
Method __repr__ Fields are represented using their initial calling arguments. This allows us to create descriptive representations for serializer instances that show all the declared fields on the serializer.
Method get_initial Return a value to use when the field is being returned as a primitive value, without any object instance.
Method get_value Given the *incoming* primitive data, return the value for this field that should be validated and transformed to a native value.
Method run_validation 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.
Method run_validators Add read_only fields with defaults to value before running validators.
Method to_internal_value Dict of native values <- Dict of primitive datatypes.
Method to_representation Object instance -> Dict of primitive datatypes.
Method validate Undocumented
Class Variable default_error_messages Undocumented
Property data Undocumented
Property errors Undocumented
Property fields A dictionary of {field_name: field_instance}.
Method _read_only_defaults Undocumented
Property _readable_fields Undocumented
Property _writable_fields Undocumented

Inherited from BaseSerializer (via Serializer):

Class Method many_init 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.
Method __class_getitem__ Undocumented
Method __init__ Undocumented
Method __new__ When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.
Method is_valid Undocumented
Method save Undocumented
Instance Variable initial_data Undocumented
Instance Variable instance Undocumented
Instance Variable partial Undocumented
Property validated_data Undocumented
Instance Variable _context Undocumented
Instance Variable _validated_data Undocumented

Inherited from Field (via Serializer, BaseSerializer):

Method __deepcopy__ When cloning fields we instantiate using the arguments it was originally created with, rather than copying the complete state.
Method bind Initializes the field name and parent for the field instance. Called when a field is added to the parent serializer instance.
Method fail A helper method that simply raises a validation error.
Method get_attribute Given the *outgoing* object instance, return the primitive value that should be used for this field.
Method get_default Return the default value to use when validating data if no input is provided for this field.
Method validate_empty_values Validate empty values, and either:
Method validators.setter Undocumented
Class Variable default_validators Undocumented
Instance Variable allow_null Undocumented
Instance Variable default Undocumented
Instance Variable default_empty_html Undocumented
Instance Variable error_messages Undocumented
Instance Variable field_name Undocumented
Instance Variable help_text Undocumented
Instance Variable initial Undocumented
Instance Variable label Undocumented
Instance Variable parent Undocumented
Instance Variable read_only Undocumented
Instance Variable required Undocumented
Instance Variable source Undocumented
Instance Variable source_attrs Undocumented
Instance Variable style Undocumented
Instance Variable write_only Undocumented
Property context Returns the context as passed to the root serializer on initialization.
Property root Returns the top-level serializer for this field.
Property validators Undocumented
Instance Variable _creation_counter Undocumented
Instance Variable _validators Undocumented
def build_field(self, field_name, info, model_class, nested_depth): (source)

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

def build_nested_field(self, field_name, relation_info, nested_depth): (source)

Create nested fields for forward and reverse relationships.

def build_property_field(self, field_name, model_class): (source)

Create a read only field for model methods and properties.

def build_relational_field(self, field_name, relation_info): (source)

Create fields for forward and reverse relationships.

def build_standard_field(self, field_name, model_field): (source)

Create regular model fields.

def build_unknown_field(self, field_name, model_class): (source)

Raise an error on any unknown fields.

def build_url_field(self, field_name, model_class): (source)

Create a field representing the object's own URL.

def create(self, validated_data): (source)

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.

def get_default_field_names(self, declared_fields, model_info): (source)

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

def get_extra_kwargs(self): (source)

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

def get_field_names(self, declared_fields, info): (source)

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.

def get_fields(self): (source)

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

def get_unique_for_date_validators(self): (source)

Determine a default set of validators for the following constraints: * unique_for_date * unique_for_month * unique_for_year

def get_unique_together_validators(self): (source)

Determine a default set of validators for any unique_together constraints.

def get_uniqueness_extra_kwargs(self, field_names, declared_fields, extra_kwargs): (source)

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')

def get_validators(self): (source)

Determine the set of validators to use when instantiating serializer.

def include_extra_kwargs(self, kwargs, extra_kwargs): (source)

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

def update(self, instance, validated_data): (source)
serializer_field_mapping = (source)

Undocumented

url_field_name = (source)

Undocumented

def _get_model_fields(self, field_names, declared_fields, extra_kwargs): (source)

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