Each field in a Form class is responsible not only for validating data, but also for "cleaning" it — normalizing it to a consistent format.
Serializer fields handle converting between primitive values and internal datatypes. They also deal with validating input values, as well as retrieving and setting the values from their parent objects.
Note: The serializer fields are declared infields.py
, but by convention you should import them usingfrom rest_framework import serializers
and refer to fields asserializers.<FieldName>
.
Each serializer field class constructor takes at least these arguments. Some Field classes take additional, field-specific arguments, but the following should always be accepted:
read_only
Read-only fields are included in the API output, but should not be included in the input during create or update operations. Any 'read_only' fields that are incorrectly included in the serializer input will be ignored.
Set this toTrue
to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization.
Defaults toFalse
write_only
Set this toTrue
to ensure that the field may be used when updating or creating an instance, but is not included when serializing the representation.
Defaults toFalse
required
Normally an error will be raised if a field is not supplied during deserialization.Set to false if this field is not required to be present during deserialization.
Setting this toFalse
also allows the object attribute or dictionary key to be omitted from output when serializing the instance. If the key is not present it will simply not be included in the output representation.
Defaults toTrue
. If you're usingModel Serializer default value will beFalse
if you have specifiedblank=True
ordefault
ornull=True
at your field in yourModel
.
default
If set, this gives the default value that will be used for the field if no input value is supplied. If not set the default behavior is to not populate the attribute at all.
Thedefault
is not applied during partial update operations. In the partial update case only fields that are provided in the incoming data will have a validated value returned.
May be set to a function or other callable, in which case the value will be evaluated each time it is used. When called, it will receive no arguments. If the callable has arequires_context = True
attribute, then the serializer field will be passed as an argument.
For example:
class CurrentUserDefault: """ May be applied as a `default=...` value on a serializer field. Returns the current user. """ requires_context = True def __call__(self, serializer_field): return serializer_field.context['request'].user
When serializing the instance, default will be used if the object attribute or dictionary key is not present in the instance.
Note that setting adefault
value implies that the field is not required. Including both thedefault
andrequired
keyword arguments is invalid and will raise an error.
allow_null
Normally an error will be raised ifNone
is passed to a serializer field. Set this keyword argument toTrue
ifNone
should be considered a valid value.
Note that, without an explicitdefault
, setting this argument toTrue
will imply adefault
value ofnull
for serialization output, but does not imply a default for input deserialization.
Defaults toFalse
source
The name of the attribute that will be used to populate the field. May be a method that only takes aself
argument, such asURLField(source='get_absolute_url')
, or may use dotted notation to traverse attributes, such asEmailField(source='user.email')
.
When serializing fields with dotted notation, it may be necessary to provide adefault
value if any object is not present or is empty during attribute traversal. Beware of possible n+1 problems when using source attribute if you are accessing a relational orm model. For example:
class CommentSerializer(serializers.Serializer): email = serializers.EmailField(source="user.email")
This case would require user object to be fetched from database when it is not prefetched. If that is not wanted, be sure to be usingprefetch_related
andselect_related
methods appropriately. For more information about the methods refer todjango documentation.
The valuesource='*'
has a special meaning, and is used to indicate that the entire object should be passed through to the field. This can be useful for creating nested representations, or for fields which require access to the complete object in order to determine the output representation.
Defaults to the name of the field.
validators
A list of validator functions which should be applied to the incoming field input, and which either raise a validation error or simply return. Validator functions should typically raiseserializers.ValidationError
, but Django's built-inValidationError
is also supported for compatibility with validators defined in the Django codebase or third party Django packages.
error_messages
A dictionary of error codes to error messages.
label
A short text string that may be used as the name of the field in HTML form fields or other descriptive elements.
help_text
A text string that may be used as a description of the field in HTML form fields or other descriptive elements.
initial
A value that should be used for pre-populating the value of HTML form fields. You may pass a callable to it, just asyou may do with any regular DjangoField
:
import datetimefrom rest_framework import serializersclass ExampleSerializer(serializers.Serializer): day = serializers.DateField(initial=datetime.date.today)
style
A dictionary of key-value pairs that can be used to control how renderers should render the field.
Two examples here are'input_type'
and'base_template'
:
# Use <input type="password"> for the input.password = serializers.CharField( style={'input_type': 'password'})# Use a radio input instead of a select input.color_channel = serializers.ChoiceField( choices=['red', 'green', 'blue'], style={'base_template': 'radio.html'})
For more details see theHTML & Forms documentation.
A boolean representation.
When using HTML encoded form input be aware that omitting a value will always be treated as setting a field toFalse
, even if it has adefault=True
option specified. This is because HTML checkbox inputs represent the unchecked state by omitting the value, so REST framework treats omission as if it is an empty checkbox input.
Note that Django 2.1 removed theblank
kwarg frommodels.BooleanField
.Prior to Django 2.1models.BooleanField
fields were alwaysblank=True
. Thussince Django 2.1 defaultserializers.BooleanField
instances will be generatedwithout therequired
kwarg (i.e. equivalent torequired=True
) whereas withprevious versions of Django, defaultBooleanField
instances will be generatedwith arequired=False
option. If you want to control this behavior manually,explicitly declare theBooleanField
on the serializer class, or use theextra_kwargs
option to set therequired
flag.
Corresponds todjango.db.models.fields.BooleanField
.
Signature:BooleanField()
A text representation. Optionally validates the text to be shorter thanmax_length
and longer thanmin_length
.
Corresponds todjango.db.models.fields.CharField
ordjango.db.models.fields.TextField
.
Signature:CharField(max_length=None, min_length=None, allow_blank=False, trim_whitespace=True)
max_length
- Validates that the input contains no more than this number of characters.min_length
- Validates that the input contains no fewer than this number of characters.allow_blank
- If set toTrue
then the empty string should be considered a valid value. If set toFalse
then the empty string is considered invalid and will raise a validation error. Defaults toFalse
.trim_whitespace
- If set toTrue
then leading and trailing whitespace is trimmed. Defaults toTrue
.Theallow_null
option is also available for string fields, although its usage is discouraged in favor ofallow_blank
. It is valid to set bothallow_blank=True
andallow_null=True
, but doing so means that there will be two differing types of empty value permissible for string representations, which can lead to data inconsistencies and subtle application bugs.
A text representation, validates the text to be a valid e-mail address.
Corresponds todjango.db.models.fields.EmailField
Signature:EmailField(max_length=None, min_length=None, allow_blank=False)
A text representation, that validates the given value matches against a certain regular expression.
Corresponds todjango.forms.fields.RegexField
.
Signature:RegexField(regex, max_length=None, min_length=None, allow_blank=False)
The mandatoryregex
argument may either be a string, or a compiled python regular expression object.
Uses Django'sdjango.core.validators.RegexValidator
for validation.
ARegexField
that validates the input against the pattern[a-zA-Z0-9_-]+
.
Corresponds todjango.db.models.fields.SlugField
.
Signature:SlugField(max_length=50, min_length=None, allow_blank=False)
ARegexField
that validates the input against a URL matching pattern. Expects fully qualified URLs of the formhttp://<host>/<path>
.
Corresponds todjango.db.models.fields.URLField
. Uses Django'sdjango.core.validators.URLValidator
for validation.
Signature:URLField(max_length=200, min_length=None, allow_blank=False)
A field that ensures the input is a valid UUID string. Theto_internal_value
method will return auuid.UUID
instance. On output the field will return a string in the canonical hyphenated format, for example:
"de305d54-75b4-431b-adb2-eb6b9e546013"
Signature:UUIDField(format='hex_verbose')
format
: Determines the representation format of the uuid value'hex_verbose'
- The canonical hex representation, including hyphens:"5ce0e9a5-5ffa-654b-cee0-1238041fb31a"
'hex'
- The compact hex representation of the UUID, not including hyphens:"5ce0e9a55ffa654bcee01238041fb31a"
'int'
- A 128 bit integer representation of the UUID:"123456789012312313134124512351145145114"
'urn'
- RFC 4122 URN representation of the UUID:"urn:uuid:5ce0e9a5-5ffa-654b-cee0-1238041fb31a"
Changing theformat
parameters only affects representation values. All formats are accepted byto_internal_value
A field whose choices are limited to the filenames in a certain directory on the filesystem
Corresponds todjango.forms.fields.FilePathField
.
Signature:FilePathField(path, match=None, recursive=False, allow_files=True, allow_folders=False, required=None, **kwargs)
path
- The absolute filesystem path to a directory from which this FilePathField should get its choice.match
- A regular expression, as a string, that FilePathField will use to filter filenames.recursive
- Specifies whether all subdirectories of path should be included. Default isFalse
.allow_files
- Specifies whether files in the specified location should be included. Default isTrue
. Either this orallow_folders
must beTrue
.allow_folders
- Specifies whether folders in the specified location should be included. Default isFalse
. Either this orallow_files
must beTrue
.A field that ensures the input is a valid IPv4 or IPv6 string.
Corresponds todjango.forms.fields.IPAddressField
anddjango.forms.fields.GenericIPAddressField
.
Signature:IPAddressField(protocol='both', unpack_ipv4=False, **options)
protocol
Limits valid inputs to the specified protocol. Accepted values are 'both' (default), 'IPv4' or 'IPv6'. Matching is case-insensitive.unpack_ipv4
Unpacks IPv4 mapped addresses like ::ffff:192.0.2.1. If this option is enabled that address would be unpacked to 192.0.2.1. Default is disabled. Can only be used when protocol is set to 'both'.An integer representation.
Corresponds todjango.db.models.fields.IntegerField
,django.db.models.fields.SmallIntegerField
,django.db.models.fields.PositiveIntegerField
anddjango.db.models.fields.PositiveSmallIntegerField
.
Signature:IntegerField(max_value=None, min_value=None)
max_value
Validate that the number provided is no greater than this value.min_value
Validate that the number provided is no less than this value.A floating point representation.
Corresponds todjango.db.models.fields.FloatField
.
Signature:FloatField(max_value=None, min_value=None)
max_value
Validate that the number provided is no greater than this value.min_value
Validate that the number provided is no less than this value.A decimal representation, represented in Python by aDecimal
instance.
Corresponds todjango.db.models.fields.DecimalField
.
Signature:DecimalField(max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None)
max_digits
The maximum number of digits allowed in the number. It must be eitherNone
or an integer greater than or equal todecimal_places
.decimal_places
The number of decimal places to store with the number.coerce_to_string
Set toTrue
if string values should be returned for the representation, orFalse
ifDecimal
objects should be returned. Defaults to the same value as theCOERCE_DECIMAL_TO_STRING
settings key, which will beTrue
unless overridden. IfDecimal
objects are returned by the serializer, then the final output format will be determined by the renderer. Note that settinglocalize
will force the value toTrue
.max_value
Validate that the number provided is no greater than this value. Should be an integer orDecimal
object.min_value
Validate that the number provided is no less than this value. Should be an integer orDecimal
object.localize
Set toTrue
to enable localization of input and output based on the current locale. This will also forcecoerce_to_string
toTrue
. Defaults toFalse
. Note that data formatting is enabled if you have setUSE_L10N=True
in your settings file.rounding
Sets the rounding mode used when quantizing to the configured precision. Valid values aredecimal
module rounding modes. Defaults toNone
.normalize_output
Will normalize the decimal value when serialized. This will strip all trailing zeroes and change the value's precision to the minimum required precision to be able to represent the value without losing data. Defaults toFalse
.To validate numbers up to 999 with a resolution of 2 decimal places, you would use:
serializers.DecimalField(max_digits=5, decimal_places=2)
And to validate numbers up to anything less than one billion with a resolution of 10 decimal places:
serializers.DecimalField(max_digits=19, decimal_places=10)
A date and time representation.
Corresponds todjango.db.models.fields.DateTimeField
.
Signature:DateTimeField(format=api_settings.DATETIME_FORMAT, input_formats=None, default_timezone=None)
format
- A string representing the output format. If not specified, this defaults to the same value as theDATETIME_FORMAT
settings key, which will be'iso-8601'
unless set. Setting to a format string indicates thatto_representation
return values should be coerced to string output. Format strings are described below. Setting this value toNone
indicates that Pythondatetime
objects should be returned byto_representation
. In this case the datetime encoding will be determined by the renderer.input_formats
- A list of strings representing the input formats which may be used to parse the date. If not specified, theDATETIME_INPUT_FORMATS
setting will be used, which defaults to['iso-8601']
.default_timezone
- Atzinfo
subclass (zoneinfo
orpytz
) representing the timezone. If not specified and theUSE_TZ
setting is enabled, this defaults to thecurrent timezone. IfUSE_TZ
is disabled, then datetime objects will be naive.DateTimeField
format strings.Format strings may either bePython strftime formats which explicitly specify the format, or the special string'iso-8601'
, which indicates thatISO 8601 style datetimes should be used. (eg'2013-01-29T12:34:56.000000Z'
)
When a value ofNone
is used for the formatdatetime
objects will be returned byto_representation
and the final output representation will be determined by the renderer class.
auto_now
andauto_now_add
model fields.When usingModelSerializer
orHyperlinkedModelSerializer
, note that any model fields withauto_now=True
orauto_now_add=True
will use serializer fields that areread_only=True
by default.
If you want to override this behavior, you'll need to declare theDateTimeField
explicitly on the serializer. For example:
class CommentSerializer(serializers.ModelSerializer): created = serializers.DateTimeField() class Meta: model = Comment
A date representation.
Corresponds todjango.db.models.fields.DateField
Signature:DateField(format=api_settings.DATE_FORMAT, input_formats=None)
format
- A string representing the output format. If not specified, this defaults to the same value as theDATE_FORMAT
settings key, which will be'iso-8601'
unless set. Setting to a format string indicates thatto_representation
return values should be coerced to string output. Format strings are described below. Setting this value toNone
indicates that Pythondate
objects should be returned byto_representation
. In this case the date encoding will be determined by the renderer.input_formats
- A list of strings representing the input formats which may be used to parse the date. If not specified, theDATE_INPUT_FORMATS
setting will be used, which defaults to['iso-8601']
.DateField
format stringsFormat strings may either bePython strftime formats which explicitly specify the format, or the special string'iso-8601'
, which indicates thatISO 8601 style dates should be used. (eg'2013-01-29'
)
A time representation.
Corresponds todjango.db.models.fields.TimeField
Signature:TimeField(format=api_settings.TIME_FORMAT, input_formats=None)
format
- A string representing the output format. If not specified, this defaults to the same value as theTIME_FORMAT
settings key, which will be'iso-8601'
unless set. Setting to a format string indicates thatto_representation
return values should be coerced to string output. Format strings are described below. Setting this value toNone
indicates that Pythontime
objects should be returned byto_representation
. In this case the time encoding will be determined by the renderer.input_formats
- A list of strings representing the input formats which may be used to parse the date. If not specified, theTIME_INPUT_FORMATS
setting will be used, which defaults to['iso-8601']
.TimeField
format stringsFormat strings may either bePython strftime formats which explicitly specify the format, or the special string'iso-8601'
, which indicates thatISO 8601 style times should be used. (eg'12:34:56.000000'
)
A Duration representation.Corresponds todjango.db.models.fields.DurationField
Thevalidated_data
for these fields will contain adatetime.timedelta
instance.The representation is a string following this format'[DD] [HH:[MM:]]ss[.uuuuuu]'
.
Signature:DurationField(max_value=None, min_value=None)
max_value
Validate that the duration provided is no greater than this value.min_value
Validate that the duration provided is no less than this value.A field that can accept a value out of a limited set of choices.
Used byModelSerializer
to automatically generate fields if the corresponding model field includes achoices=…
argument.
Signature:ChoiceField(choices)
choices
- A list of valid values, or a list of(key, display_name)
tuples.allow_blank
- If set toTrue
then the empty string should be considered a valid value. If set toFalse
then the empty string is considered invalid and will raise a validation error. Defaults toFalse
.html_cutoff
- If set this will be the maximum number of choices that will be displayed by a HTML select drop down. Can be used to ensure that automatically generated ChoiceFields with very large possible selections do not prevent a template from rendering. Defaults toNone
.html_cutoff_text
- If set this will display a textual indicator if the maximum number of items have been cutoff in an HTML select drop down. Defaults to"More than {count} items…"
Both theallow_blank
andallow_null
are valid options onChoiceField
, although it is highly recommended that you only use one and not both.allow_blank
should be preferred for textual choices, andallow_null
should be preferred for numeric or other non-textual choices.
A field that can accept a set of zero, one or many values, chosen from a limited set of choices. Takes a single mandatory argument.to_internal_value
returns aset
containing the selected values.
Signature:MultipleChoiceField(choices)
choices
- A list of valid values, or a list of(key, display_name)
tuples.allow_blank
- If set toTrue
then the empty string should be considered a valid value. If set toFalse
then the empty string is considered invalid and will raise a validation error. Defaults toFalse
.html_cutoff
- If set this will be the maximum number of choices that will be displayed by a HTML select drop down. Can be used to ensure that automatically generated ChoiceFields with very large possible selections do not prevent a template from rendering. Defaults toNone
.html_cutoff_text
- If set this will display a textual indicator if the maximum number of items have been cutoff in an HTML select drop down. Defaults to"More than {count} items…"
As withChoiceField
, both theallow_blank
andallow_null
options are valid, although it is highly recommended that you only use one and not both.allow_blank
should be preferred for textual choices, andallow_null
should be preferred for numeric or other non-textual choices.
TheFileField
andImageField
classes are only suitable for use withMultiPartParser
orFileUploadParser
. Most parsers, such as e.g. JSON don't support file uploads.Django's regularFILE_UPLOAD_HANDLERS are used for handling uploaded files.
A file representation. Performs Django's standard FileField validation.
Corresponds todjango.forms.fields.FileField
.
Signature:FileField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)
max_length
- Designates the maximum length for the file name.allow_empty_file
- Designates if empty files are allowed.use_url
- If set toTrue
then URL string values will be used for the output representation. If set toFalse
then filename string values will be used for the output representation. Defaults to the value of theUPLOADED_FILES_USE_URL
settings key, which isTrue
unless set otherwise.An image representation. Validates the uploaded file content as matching a known image format.
Corresponds todjango.forms.fields.ImageField
.
Signature:ImageField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)
max_length
- Designates the maximum length for the file name.allow_empty_file
- Designates if empty files are allowed.use_url
- If set toTrue
then URL string values will be used for the output representation. If set toFalse
then filename string values will be used for the output representation. Defaults to the value of theUPLOADED_FILES_USE_URL
settings key, which isTrue
unless set otherwise.Requires either thePillow
package orPIL
package. ThePillow
package is recommended, asPIL
is no longer actively maintained.
A field class that validates a list of objects.
Signature:ListField(child=<A_FIELD_INSTANCE>, allow_empty=True, min_length=None, max_length=None)
child
- A field instance that should be used for validating the objects in the list. If this argument is not provided then objects in the list will not be validated.allow_empty
- Designates if empty lists are allowed.min_length
- Validates that the list contains no fewer than this number of elements.max_length
- Validates that the list contains no more than this number of elements.For example, to validate a list of integers you might use something like the following:
scores = serializers.ListField( child=serializers.IntegerField(min_value=0, max_value=100))
TheListField
class also supports a declarative style that allows you to write reusable list field classes.
class StringListField(serializers.ListField): child = serializers.CharField()
We can now reuse our customStringListField
class throughout our application, without having to provide achild
argument to it.
A field class that validates a dictionary of objects. The keys inDictField
are always assumed to be string values.
Signature:DictField(child=<A_FIELD_INSTANCE>, allow_empty=True)
child
- A field instance that should be used for validating the values in the dictionary. If this argument is not provided then values in the mapping will not be validated.allow_empty
- Designates if empty dictionaries are allowed.For example, to create a field that validates a mapping of strings to strings, you would write something like this:
document = DictField(child=CharField())
You can also use the declarative style, as withListField
. For example:
class DocumentField(DictField): child = CharField()
A preconfiguredDictField
that is compatible with Django's postgresHStoreField
.
Signature:HStoreField(child=<A_FIELD_INSTANCE>, allow_empty=True)
child
- A field instance that is used for validating the values in the dictionary. The default child field accepts both empty strings and null values.allow_empty
- Designates if empty dictionaries are allowed.Note that the child fieldmust be an instance ofCharField
, as the hstore extension stores values as strings.
A field class that validates that the incoming data structure consists of valid JSON primitives. In its alternate binary mode, it will represent and validate JSON-encoded binary strings.
Signature:JSONField(binary, encoder)
binary
- If set toTrue
then the field will output and validate a JSON encoded string, rather than a primitive data structure. Defaults toFalse
.encoder
- Use this JSON encoder to serialize input object. Defaults toNone
.A field class that simply returns the value of the field without modification.
This field is used by default withModelSerializer
when including field names that relate to an attribute rather than a model field.
Signature:ReadOnlyField()
For example, ifhas_expired
was a property on theAccount
model, then the following serializer would automatically generate it as aReadOnlyField
:
class AccountSerializer(serializers.ModelSerializer): class Meta: model = Account fields = ['id', 'account_name', 'has_expired']
A field class that does not take a value based on user input, but instead takes its value from a default value or callable.
Signature:HiddenField()
For example, to include a field that always provides the current time as part of the serializer validated data, you would use the following:
modified = serializers.HiddenField(default=timezone.now)
TheHiddenField
class is usually only needed if you have some validation that needs to run based on some pre-provided field values, but you do not want to expose all of those fields to the end user.
For further examples onHiddenField
see thevalidators documentation.
Note:HiddenField()
does not appear inpartial=True
serializer (when makingPATCH
request).
A generic field that can be tied to any arbitrary model field. TheModelField
class delegates the task of serialization/deserialization to its associated model field. This field can be used to create serializer fields for custom model fields, without having to create a new custom serializer field.
This field is used byModelSerializer
to correspond to custom model field classes.
Signature:ModelField(model_field=<Django ModelField instance>)
TheModelField
class is generally intended for internal use, but can be used by your API if needed. In order to properly instantiate aModelField
, it must be passed a field that is attached to an instantiated model. For example:ModelField(model_field=MyModel()._meta.get_field('custom_field'))
This is a read-only field. It 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:SerializerMethodField(method_name=None)
method_name
- The name of the method on the serializer to be called. If not included this defaults toget_<field_name>
.The serializer method referred to by themethod_name
argument should accept a single argument (in addition toself
), which is the object being serialized. It should return whatever you want to be included in the serialized representation of the object. For example:
from django.contrib.auth.models import Userfrom django.utils.timezone import nowfrom rest_framework import serializersclass UserSerializer(serializers.ModelSerializer): days_since_joined = serializers.SerializerMethodField() class Meta: model = User fields = '__all__' def get_days_since_joined(self, obj): return (now() - obj.date_joined).days
If you want to create a custom field, you'll need to subclassField
and then override either one or both of the.to_representation()
and.to_internal_value()
methods. These two methods are used to convert between the initial datatype, and a primitive, serializable datatype. Primitive datatypes will typically be any of a number, string, boolean,date
/time
/datetime
orNone
. They may also be any list or dictionary like object that only contains other primitive objects. Other types might be supported, depending on the renderer that you are using.
The.to_representation()
method is called to convert the initial datatype into a primitive, serializable datatype.
The.to_internal_value()
method is called to restore a primitive datatype into its internal python representation. This method should raise aserializers.ValidationError
if the data is invalid.
Let's look at an example of serializing a class that represents an RGB color value:
class Color: """ A color represented in the RGB colorspace. """ def __init__(self, red, green, blue): assert(red >= 0 and green >= 0 and blue >= 0) assert(red < 256 and green < 256 and blue < 256) self.red, self.green, self.blue = red, green, blueclass ColorField(serializers.Field): """ Color objects are serialized into 'rgb(#, #, #)' notation. """ def to_representation(self, value): return "rgb(%d, %d, %d)" % (value.red, value.green, value.blue) def to_internal_value(self, data): data = data.strip('rgb(').rstrip(')') red, green, blue = [int(col) for col in data.split(',')] return Color(red, green, blue)
By default field values are treated as mapping to an attribute on the object. If you need to customize how the field value is accessed and set you need to override.get_attribute()
and/or.get_value()
.
As an example, let's create a field that can be used to represent the class name of the object being serialized:
class ClassNameField(serializers.Field): def get_attribute(self, instance): # We pass the object instance onto `to_representation`, # not just the field attribute. return instance def to_representation(self, value): """ Serialize the value's class name. """ return value.__class__.__name__
OurColorField
class above currently does not perform any data validation.To indicate invalid data, we should raise aserializers.ValidationError
, like so:
def to_internal_value(self, data): if not isinstance(data, str): msg = 'Incorrect type. Expected a string, but got %s' raise ValidationError(msg % type(data).__name__) if not re.match(r'^rgb\([0-9]+,[0-9]+,[0-9]+\)$', data): raise ValidationError('Incorrect format. Expected `rgb(#,#,#)`.') data = data.strip('rgb(').rstrip(')') red, green, blue = [int(col) for col in data.split(',')] if any([col > 255 or col < 0 for col in (red, green, blue)]): raise ValidationError('Value out of range. Must be between 0 and 255.') return Color(red, green, blue)
The.fail()
method is a shortcut for raisingValidationError
that takes a message string from theerror_messages
dictionary. For example:
default_error_messages = { 'incorrect_type': 'Incorrect type. Expected a string, but got {input_type}', 'incorrect_format': 'Incorrect format. Expected `rgb(#,#,#)`.', 'out_of_range': 'Value out of range. Must be between 0 and 255.'}def to_internal_value(self, data): if not isinstance(data, str): self.fail('incorrect_type', input_type=type(data).__name__) if not re.match(r'^rgb\([0-9]+,[0-9]+,[0-9]+\)$', data): self.fail('incorrect_format') data = data.strip('rgb(').rstrip(')') red, green, blue = [int(col) for col in data.split(',')] if any([col > 255 or col < 0 for col in (red, green, blue)]): self.fail('out_of_range') return Color(red, green, blue)
This style keeps your error messages cleaner and more separated from your code, and should be preferred.
source='*'
Here we'll take an example of aflatDataPoint
model withx_coordinate
andy_coordinate
attributes.
class DataPoint(models.Model): label = models.CharField(max_length=50) x_coordinate = models.SmallIntegerField() y_coordinate = models.SmallIntegerField()
Using a custom field andsource='*'
we can provide a nested representation ofthe coordinate pair:
class CoordinateField(serializers.Field): def to_representation(self, value): ret = { "x": value.x_coordinate, "y": value.y_coordinate } return ret def to_internal_value(self, data): ret = { "x_coordinate": data["x"], "y_coordinate": data["y"], } return retclass DataPointSerializer(serializers.ModelSerializer): coordinates = CoordinateField(source='*') class Meta: model = DataPoint fields = ['label', 'coordinates']
Note that this example doesn't handle validation. Partly for that reason, in areal project, the coordinate nesting might be better handled with a nested serializerusingsource='*'
, with twoIntegerField
instances, each with their ownsource
pointing to the relevant field.
The key points from the example, though, are:
to_representation
is passed the entireDataPoint
object and must map from thatto the desired output.
>>> instance = DataPoint(label='Example', x_coordinate=1, y_coordinate=2)>>> out_serializer = DataPointSerializer(instance)>>> out_serializer.dataReturnDict([('label', 'Example'), ('coordinates', {'x': 1, 'y': 2})])
Unless our field is to be read-only,to_internal_value
must map back to a dictsuitable for updating our target object. Withsource='*'
, the return fromto_internal_value
will update the root validated data dictionary, rather than a single key.
>>> data = {... "label": "Second Example",... "coordinates": {... "x": 3,... "y": 4,... }... }>>> in_serializer = DataPointSerializer(data=data)>>> in_serializer.is_valid()True>>> in_serializer.validated_dataOrderedDict([('label', 'Second Example'), ('y_coordinate', 4), ('x_coordinate', 3)])
For completeness lets do the same thing again but with the nested serializerapproach suggested above:
class NestedCoordinateSerializer(serializers.Serializer): x = serializers.IntegerField(source='x_coordinate') y = serializers.IntegerField(source='y_coordinate')class DataPointSerializer(serializers.ModelSerializer): coordinates = NestedCoordinateSerializer(source='*') class Meta: model = DataPoint fields = ['label', 'coordinates']
Here the mapping between the target and source attribute pairs (x
andx_coordinate
,y
andy_coordinate
) is handled in theIntegerField
declarations. It's ourNestedCoordinateSerializer
that takessource='*'
.
Our newDataPointSerializer
exhibits the same behavior as the custom fieldapproach.
Serializing:
>>> out_serializer = DataPointSerializer(instance)>>> out_serializer.dataReturnDict([('label', 'testing'), ('coordinates', OrderedDict([('x', 1), ('y', 2)]))])
Deserializing:
>>> in_serializer = DataPointSerializer(data=data)>>> in_serializer.is_valid()True>>> in_serializer.validated_dataOrderedDict([('label', 'still testing'), ('x_coordinate', 3), ('y_coordinate', 4)])
But we also get the built-in validation for free:
>>> invalid_data = {... "label": "still testing",... "coordinates": {... "x": 'a',... "y": 'b',... }... }>>> invalid_serializer = DataPointSerializer(data=invalid_data)>>> invalid_serializer.is_valid()False>>> invalid_serializer.errorsReturnDict([('coordinates', {'x': ['A valid integer is required.'], 'y': ['A valid integer is required.']})])
For this reason, the nested serializer approach would be the first to try. Youwould use the custom field approach when the nested serializer becomes infeasibleor overly complex.
The following third party packages are also available.
Thedrf-compound-fields package provides "compound" serializer fields, such as lists of simple values, which can be described by other fields rather than serializers with themany=True
option. Also provided are fields for typed dictionaries and values that can be either a specific type or a list of items of that type.
Thedrf-extra-fields package provides extra serializer fields for REST framework, includingBase64ImageField
andPointField
classes.
thedjangorestframework-recursive package provides aRecursiveField
for serializing and deserializing recursive structures
Thedjango-rest-framework-gis package provides geographic addons for django rest framework like aGeometryField
field and a GeoJSON serializer.
Thedjango-rest-framework-hstore package provides anHStoreField
to supportdjango-hstoreDictionaryField
model field.