Core Arguments
All field types accept the following arguments:Field is used for serialization but not deserialization.
Field is used for deserialization but not serialization.
Field must be present in input data during deserialization.
Default value if no input is supplied. Can be a value or callable.
Accept
None as a valid value.Attribute name to use for populating the field. Use
'*' for the entire object.List of validator functions to apply to the field input.
Dictionary of error codes to error messages.
Short text string for HTML form fields.
Description text for HTML form fields.
Value for pre-populating HTML form fields.
Key-value pairs to control renderer display.
Boolean Fields
BooleanField
A boolean representation.True, False, 't', 'f', 'true', 'false', '1', '0', 1, 0, 'yes', 'no', 'on', 'off'
Output: True or False
Example:
String Fields
CharField
A text representation.Maximum number of characters.
Minimum number of characters.
Allow empty string as valid value.
Trim leading and trailing whitespace.
EmailField
A text representation that validates email addresses.RegexField
A text representation that validates against a regular expression.Regular expression string or compiled pattern object.
SlugField
Validates against the pattern[a-zA-Z0-9_-]+.
Allow Unicode letters in addition to ASCII.
URLField
Validates fully qualified URLs.UUIDField
Ensures input is a valid UUID string.UUID format:
'hex_verbose', 'hex', 'int', or 'urn'.FilePathField
Choices limited to filenames in a directory.Absolute filesystem path to directory.
Regular expression to filter filenames.
Include all subdirectories.
Include files in choices.
Include folders in choices.
IPAddressField
Validates IPv4 or IPv6 addresses.Accepted values:
'both', 'IPv4', or 'IPv6'.Unpack IPv4 mapped addresses like
::ffff:192.0.2.1.Numeric Fields
IntegerField
An integer representation.Maximum value allowed.
Minimum value allowed.
BigIntegerField
A biginteger representation.Return string values. Defaults to
COERCE_BIGINT_TO_STRING setting.FloatField
A floating point representation.DecimalField
A decimal representation.Maximum number of digits allowed.
Number of decimal places to store.
Return string values. Defaults to
COERCE_DECIMAL_TO_STRING setting.Maximum value allowed.
Minimum value allowed.
Enable localization of input and output.
Decimal rounding mode.
Strip trailing zeros from output.
Date and Time Fields
DateTimeField
A date and time representation.Output format: strftime string or
'iso-8601'. Defaults to DATETIME_FORMAT setting.List of input format strings. Defaults to
DATETIME_INPUT_FORMATS setting.Timezone for naive datetimes. Defaults to current timezone if
USE_TZ is enabled.DateField
A date representation.TimeField
A time representation.DurationField
A duration representation.Output format:
'iso-8601' or 'django'. Defaults to DURATION_FORMAT setting.Choice Fields
ChoiceField
Accepts a value from a limited set of choices.List of valid values or
(key, display_name) tuples.Allow empty string as valid value.
Maximum choices displayed in HTML select.
Text indicator for cutoff choices.
MultipleChoiceField
Accepts a list of values from a set of choices.File Upload Fields
FileField
A file representation.Maximum length for filename.
Allow empty files.
Use URL strings in output representation.
ImageField
An image representation with validation.Composite Fields
ListField
Validates a list of objects.Field instance for validating list items.
Allow empty lists.
Minimum number of elements.
Maximum number of elements.
DictField
Validates a dictionary of objects.Field instance for validating dictionary values.
HStoreField
PostgreSQL HStore field.JSONField
Validates JSON primitives.Output JSON-encoded string instead of primitive data.
Custom JSON encoder class.
Miscellaneous Fields
ReadOnlyField
Returns field value without modification.HiddenField
Takes value from default or callable, not user input.ModelField
Delegates to an arbitrary Django model field.SerializerMethodField
Read-only field that calls a method on the serializer.Method name to call. Defaults to
get_<field_name>.