mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-15 06:07:41 +03:00
1009 lines
54 KiB
HTML
1009 lines
54 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
<meta charset="utf-8">
|
|
<title>Serializer fields - Django REST framework</title>
|
|
<link href="../../img/favicon.ico" rel="icon" type="image/x-icon">
|
|
<link rel="canonical" href="http://www.django-rest-framework.org/api-guide/fields/" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="description" content="Django, API, REST, Serializer fields">
|
|
<meta name="author" content="Tom Christie">
|
|
|
|
<!-- Le styles -->
|
|
<link href="../../css/prettify.css" rel="stylesheet">
|
|
<link href="../../css/bootstrap.css" rel="stylesheet">
|
|
<link href="../../css/bootstrap-responsive.css" rel="stylesheet">
|
|
<link href="../../css/default.css" rel="stylesheet">
|
|
|
|
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
|
|
<!--[if lt IE 9]>
|
|
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
|
|
<script type="text/javascript">
|
|
var _gaq = _gaq || [];
|
|
_gaq.push(['_setAccount', 'UA-18852272-2']);
|
|
_gaq.push(['_trackPageview']);
|
|
|
|
(function() {
|
|
var ga = document.createElement('script');
|
|
ga.type = 'text/javascript';
|
|
ga.async = true;
|
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
|
var s = document.getElementsByTagName('script')[0];
|
|
s.parentNode.insertBefore(ga, s);
|
|
})();
|
|
</script>
|
|
|
|
<style>
|
|
span.fusion-wrap a {
|
|
display: block;
|
|
margin-top: 10px;
|
|
color: black;
|
|
}
|
|
a.fusion-poweredby {
|
|
display: block;
|
|
margin-top: 10px;
|
|
}
|
|
@media (max-width: 767px) {
|
|
div.promo {
|
|
display: none;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body onload="prettyPrint()" class="-page">
|
|
|
|
<div class="wrapper">
|
|
|
|
<div class="navbar navbar-inverse navbar-fixed-top">
|
|
<div class="navbar-inner">
|
|
<div class="container-fluid">
|
|
<a class="repo-link btn btn-primary btn-small" href="https://github.com/tomchristie/django-rest-framework/tree/master">GitHub</a>
|
|
<a class="repo-link btn btn-inverse btn-small " rel="prev" href="../relations">
|
|
Next <i class="icon-arrow-right icon-white"></i>
|
|
</a>
|
|
<a class="repo-link btn btn-inverse btn-small " rel="next" href="../serializers">
|
|
<i class="icon-arrow-left icon-white"></i> Previous
|
|
</a>
|
|
<a class="repo-link btn btn-inverse btn-small" href="#searchModal" data-toggle="modal"><i class="icon-search icon-white"></i> Search</a>
|
|
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
</a>
|
|
<a class="brand" href="http://www.django-rest-framework.org">Django REST framework</a>
|
|
<div class="nav-collapse collapse">
|
|
|
|
<!-- Main navigation -->
|
|
<ul class="nav navbar-nav">
|
|
<li ><a href="/">Home</a></li>
|
|
|
|
<li class="dropdown">
|
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Tutorial <b class="caret"></b></a>
|
|
<ul class="dropdown-menu">
|
|
|
|
<li >
|
|
<a href="../../tutorial/quickstart">Quickstart</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/1-serialization">1 - Serialization</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/2-requests-and-responses">2 - Requests and responses</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/3-class-based-views">3 - Class based views</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/4-authentication-and-permissions">4 - Authentication and permissions</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/5-relationships-and-hyperlinked-apis">5 - Relationships and hyperlinked APIs</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/6-viewsets-and-routers">6 - Viewsets and routers</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown active">
|
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">API Guide <b class="caret"></b></a>
|
|
<ul class="dropdown-menu">
|
|
|
|
<li >
|
|
<a href="../requests">Requests</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../responses">Responses</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../views">Views</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../generic-views">Generic views</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../viewsets">Viewsets</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../routers">Routers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../parsers">Parsers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../renderers">Renderers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../serializers">Serializers</a>
|
|
</li>
|
|
|
|
<li class="active" >
|
|
<a href=".">Serializer fields</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../relations">Serializer relations</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../validators">Validators</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../authentication">Authentication</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../permissions">Permissions</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../throttling">Throttling</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../filtering">Filtering</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../pagination">Pagination</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../content-negotiation">Content negotiation</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../metadata">Metadata</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../format-suffixes">Format suffixes</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../reverse">Returning URLs</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../exceptions">Exceptions</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../status-codes">Status codes</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../testing">Testing</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../settings">Settings</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Topics <b class="caret"></b></a>
|
|
<ul class="dropdown-menu">
|
|
|
|
<li >
|
|
<a href="../../topics/documenting-your-api">Documenting your API</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/ajax-csrf-cors">AJAX, CSRF & CORS</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/browser-enhancements">Browser enhancements</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/browsable-api">The Browsable API</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/rest-hypermedia-hateoas">REST, Hypermedia & HATEOAS</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/third-party-resources">Third Party Resources</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/contributing">Contributing to REST framework</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/project-management">Project management</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/rest-framework-2-announcement">2.0 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/2.2-announcement">2.2 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/2.3-announcement">2.3 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/2.4-announcement">2.4 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/3.0-announcement">3.0 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/kickstarter-announcement">Kickstarter Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/release-notes">Release Notes</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/credits">Credits</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
<!--/.nav-collapse -->
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="body-content">
|
|
<div class="container-fluid">
|
|
|
|
<!-- Search Modal -->
|
|
<div id="searchModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
|
|
<div class="modal-header">
|
|
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
|
<h3 id="myModalLabel">Documentation search</h3>
|
|
</div>
|
|
|
|
<div class="modal-body">
|
|
<!-- Custom google search -->
|
|
<script>
|
|
(function() {
|
|
var cx = '015016005043623903336:rxraeohqk6w';
|
|
var gcse = document.createElement('script');
|
|
gcse.type = 'text/javascript';
|
|
gcse.async = true;
|
|
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
|
|
'//www.google.com/cse/cse.js?cx=' + cx;
|
|
var s = document.getElementsByTagName('script')[0];
|
|
s.parentNode.insertBefore(gcse, s);
|
|
})();
|
|
</script>
|
|
<gcse:search></gcse:search>
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row-fluid">
|
|
|
|
<div class="span3">
|
|
<!-- TODO
|
|
<p style="margin-top: -12px">
|
|
<a class="btn btn-mini btn-primary" style="width: 60px">« previous</a>
|
|
<a class="btn btn-mini btn-primary" style="float: right; margin-right: 8px; width: 60px;">next »</a>
|
|
</p>
|
|
-->
|
|
<div id="table-of-contents">
|
|
<ul class="nav nav-list side-nav well sidebar-nav-fixed">
|
|
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#serializer-fields">Serializer fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#core-arguments">Core arguments</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#boolean-fields">Boolean fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#booleanfield">BooleanField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#nullbooleanfield">NullBooleanField</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#string-fields">String fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#charfield">CharField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#emailfield">EmailField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#regexfield">RegexField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#slugfield">SlugField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#urlfield">URLField</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#numeric-fields">Numeric fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#integerfield">IntegerField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#floatfield">FloatField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#decimalfield">DecimalField</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#date-and-time-fields">Date and time fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#datetimefield">DateTimeField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#datefield">DateField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#timefield">TimeField</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#choice-selection-fields">Choice selection fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#choicefield">ChoiceField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#multiplechoicefield">MultipleChoiceField</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#file-upload-fields">File upload fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#parsers-and-file-uploads">Parsers and file uploads.</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#filefield">FileField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#imagefield">ImageField</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#composite-fields">Composite fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#listfield">ListField</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#miscellaneous-fields">Miscellaneous fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#readonlyfield">ReadOnlyField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#hiddenfield">HiddenField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#modelfield">ModelField</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#serializermethodfield">SerializerMethodField</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#custom-fields">Custom fields</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#examples">Examples</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="#third-party-packages">Third party packages</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="#drf-compound-fields">DRF Compound Fields</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#drf-extra-fields">DRF Extra Fields</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#django-rest-framework-gis">django-rest-framework-gis</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="#django-rest-framework-hstore">django-rest-framework-hstore</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div id="main-content" class="span9">
|
|
|
|
|
|
<a class="github" href="https://github.com/tomchristie/django-rest-framework/tree/master/rest_framework/fields.py">
|
|
<span class="label label-info">fields.py</span>
|
|
</a>
|
|
|
|
|
|
|
|
<hr />
|
|
<p><strong>Note</strong>: This is the documentation for the <strong>version 3.0</strong> of REST framework. Documentation for <a href="http://tomchristie.github.io/rest-framework-2-docs/">version 2.4</a> is also available.</p>
|
|
<hr />
|
|
<h1 id="serializer-fields">Serializer fields</h1>
|
|
<blockquote>
|
|
<p>Each field in a Form class is responsible not only for validating data, but also for "cleaning" it — normalizing it to a consistent format.</p>
|
|
<p>— <a href="https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.cleaned_data">Django documentation</a></p>
|
|
</blockquote>
|
|
<p>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.</p>
|
|
<hr />
|
|
<p><strong>Note:</strong> The serializer fields are declared in <code>fields.py</code>, but by convention you should import them using <code>from rest_framework import serializers</code> and refer to fields as <code>serializers.<FieldName></code>.</p>
|
|
<hr />
|
|
<h2 id="core-arguments">Core arguments</h2>
|
|
<p>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:</p>
|
|
<h3 id="read_only"><code>read_only</code></h3>
|
|
<p>Set this to <code>True</code> to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization.</p>
|
|
<p>Defaults to <code>False</code></p>
|
|
<h3 id="write_only"><code>write_only</code></h3>
|
|
<p>Set this to <code>True</code> to ensure that the field may be used when updating or creating an instance, but is not included when serializing the representation.</p>
|
|
<p>Defaults to <code>False</code></p>
|
|
<h3 id="required"><code>required</code></h3>
|
|
<p>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.</p>
|
|
<p>Setting this to <code>False</code> 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.</p>
|
|
<p>Defaults to <code>True</code>.</p>
|
|
<h3 id="allow_null"><code>allow_null</code></h3>
|
|
<p>Normally an error will be raised if <code>None</code> is passed to a serializer field. Set this keyword argument to <code>True</code> if <code>None</code> should be considered a valid value.</p>
|
|
<p>Defaults to <code>False</code></p>
|
|
<h3 id="default"><code>default</code></h3>
|
|
<p>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.</p>
|
|
<p>May be set to a function or other callable, in which case the value will be evaluated each time it is used.</p>
|
|
<p>Note that setting a <code>default</code> value implies that the field is not required. Including both the <code>default</code> and <code>required</code> keyword arguments is invalid and will raise an error.</p>
|
|
<h3 id="source"><code>source</code></h3>
|
|
<p>The name of the attribute that will be used to populate the field. May be a method that only takes a <code>self</code> argument, such as <code>URLField('get_absolute_url')</code>, or may use dotted notation to traverse attributes, such as <code>EmailField(source='user.email')</code>.</p>
|
|
<p>The value <code>source='*'</code> 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.</p>
|
|
<p>Defaults to the name of the field.</p>
|
|
<h3 id="validators"><code>validators</code></h3>
|
|
<p>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 raise <code>serializers.ValidationError</code>, but Django's built-in <code>ValidationError</code> is also supported for compatibility with validators defined in the Django codebase or third party Django packages.</p>
|
|
<h3 id="error_messages"><code>error_messages</code></h3>
|
|
<p>A dictionary of error codes to error messages.</p>
|
|
<h3 id="label"><code>label</code></h3>
|
|
<p>A short text string that may be used as the name of the field in HTML form fields or other descriptive elements.</p>
|
|
<h3 id="help_text"><code>help_text</code></h3>
|
|
<p>A text string that may be used as a description of the field in HTML form fields or other descriptive elements.</p>
|
|
<h3 id="initial"><code>initial</code></h3>
|
|
<p>A value that should be used for pre-populating the value of HTML form fields.</p>
|
|
<h3 id="style"><code>style</code></h3>
|
|
<p>A dictionary of key-value pairs that can be used to control how renderers should render the field. The API for this should still be considered experimental, and will be formalized with the 3.1 release.</p>
|
|
<p>Two options are currently used in HTML form generation, <code>'input_type'</code> and <code>'base_template'</code>.</p>
|
|
<pre><code># 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'}
|
|
}
|
|
</code></pre>
|
|
<p><strong>Note</strong>: The <code>style</code> argument replaces the old-style version 2.x <code>widget</code> keyword argument. Because REST framework 3 now uses templated HTML form generation, the <code>widget</code> option that was used to support Django built-in widgets can no longer be supported. Version 3.1 is planned to include public API support for customizing HTML form generation.</p>
|
|
<hr />
|
|
<h1 id="boolean-fields">Boolean fields</h1>
|
|
<h2 id="booleanfield">BooleanField</h2>
|
|
<p>A boolean representation.</p>
|
|
<p>When using HTML encoded form input be aware that omitting a value will always be treated as setting a field to <code>False</code>, even if it has a <code>default=True</code> 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.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.BooleanField</code>.</p>
|
|
<p><strong>Signature:</strong> <code>BooleanField()</code></p>
|
|
<h2 id="nullbooleanfield">NullBooleanField</h2>
|
|
<p>A boolean representation that also accepts <code>None</code> as a valid value.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.NullBooleanField</code>.</p>
|
|
<p><strong>Signature:</strong> <code>NullBooleanField()</code></p>
|
|
<hr />
|
|
<h1 id="string-fields">String fields</h1>
|
|
<h2 id="charfield">CharField</h2>
|
|
<p>A text representation. Optionally validates the text to be shorter than <code>max_length</code> and longer than <code>min_length</code>.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.CharField</code> or <code>django.db.models.fields.TextField</code>.</p>
|
|
<p><strong>Signature:</strong> <code>CharField(max_length=None, min_length=None, allow_blank=False)</code></p>
|
|
<ul>
|
|
<li><code>max_length</code> - Validates that the input contains no more than this number of characters.</li>
|
|
<li><code>min_length</code> - Validates that the input contains no fewer than this number of characters.</li>
|
|
<li><code>allow_blank</code> - If set to <code>True</code> then the empty string should be considered a valid value. If set to <code>False</code> then the empty string is considered invalid and will raise a validation error. Defaults to <code>False</code>.</li>
|
|
</ul>
|
|
<p>The <code>allow_null</code> option is also available for string fields, although its usage is discouraged in favor of <code>allow_blank</code>. It is valid to set both <code>allow_blank=True</code> and <code>allow_null=True</code>, 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.</p>
|
|
<h2 id="emailfield">EmailField</h2>
|
|
<p>A text representation, validates the text to be a valid e-mail address.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.EmailField</code></p>
|
|
<p><strong>Signature:</strong> <code>EmailField(max_length=None, min_length=None, allow_blank=False)</code></p>
|
|
<h2 id="regexfield">RegexField</h2>
|
|
<p>A text representation, that validates the given value matches against a certain regular expression.</p>
|
|
<p>Corresponds to <code>django.forms.fields.RegexField</code>.</p>
|
|
<p><strong>Signature:</strong> <code>RegexField(regex, max_length=None, min_length=None, allow_blank=False)</code></p>
|
|
<p>The mandatory <code>regex</code> argument may either be a string, or a compiled python regular expression object.</p>
|
|
<p>Uses Django's <code>django.core.validators.RegexValidator</code> for validation.</p>
|
|
<h2 id="slugfield">SlugField</h2>
|
|
<p>A <code>RegexField</code> that validates the input against the pattern <code>[a-zA-Z0-9_-]+</code>.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.SlugField</code>.</p>
|
|
<p><strong>Signature:</strong> <code>SlugField(max_length=50, min_length=None, allow_blank=False)</code></p>
|
|
<h2 id="urlfield">URLField</h2>
|
|
<p>A <code>RegexField</code> that validates the input against a URL matching pattern. Expects fully qualified URLs of the form <code>http://<host>/<path></code>.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.URLField</code>. Uses Django's <code>django.core.validators.URLValidator</code> for validation.</p>
|
|
<p><strong>Signature:</strong> <code>URLField(max_length=200, min_length=None, allow_blank=False)</code></p>
|
|
<hr />
|
|
<h1 id="numeric-fields">Numeric fields</h1>
|
|
<h2 id="integerfield">IntegerField</h2>
|
|
<p>An integer representation.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.IntegerField</code>, <code>django.db.models.fields.SmallIntegerField</code>, <code>django.db.models.fields.PositiveIntegerField</code> and <code>django.db.models.fields.PositiveSmallIntegerField</code>.</p>
|
|
<p><strong>Signature</strong>: <code>IntegerField(max_value=None, min_value=None)</code></p>
|
|
<ul>
|
|
<li><code>max_value</code> Validate that the number provided is no greater than this value.</li>
|
|
<li><code>min_value</code> Validate that the number provided is no less than this value.</li>
|
|
</ul>
|
|
<h2 id="floatfield">FloatField</h2>
|
|
<p>A floating point representation.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.FloatField</code>.</p>
|
|
<p><strong>Signature</strong>: <code>FloatField(max_value=None, min_value=None)</code></p>
|
|
<ul>
|
|
<li><code>max_value</code> Validate that the number provided is no greater than this value.</li>
|
|
<li><code>min_value</code> Validate that the number provided is no less than this value.</li>
|
|
</ul>
|
|
<h2 id="decimalfield">DecimalField</h2>
|
|
<p>A decimal representation, represented in Python by a <code>Decimal</code> instance.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.DecimalField</code>.</p>
|
|
<p><strong>Signature</strong>: <code>DecimalField(max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None)</code></p>
|
|
<ul>
|
|
<li><code>max_digits</code> The maximum number of digits allowed in the number. Note that this number must be greater than or equal to decimal_places.</li>
|
|
<li><code>decimal_places</code> The number of decimal places to store with the number.</li>
|
|
<li><code>coerce_to_string</code> Set to <code>True</code> if string values should be returned for the representation, or <code>False</code> if <code>Decimal</code> objects should be returned. Defaults to the same value as the <code>COERCE_DECIMAL_TO_STRING</code> settings key, which will be <code>True</code> unless overridden. If <code>Decimal</code> objects are returned by the serializer, then the final output format will be determined by the renderer.</li>
|
|
<li><code>max_value</code> Validate that the number provided is no greater than this value.</li>
|
|
<li><code>min_value</code> Validate that the number provided is no less than this value.</li>
|
|
</ul>
|
|
<h4 id="example-usage">Example usage</h4>
|
|
<p>To validate numbers up to 999 with a resolution of 2 decimal places, you would use:</p>
|
|
<pre><code>serializers.DecimalField(max_digits=5, decimal_places=2)
|
|
</code></pre>
|
|
<p>And to validate numbers up to anything less than one billion with a resolution of 10 decimal places:</p>
|
|
<pre><code>serializers.DecimalField(max_digits=19, decimal_places=10)
|
|
</code></pre>
|
|
<p>This field also takes an optional argument, <code>coerce_to_string</code>. If set to <code>True</code> the representation will be output as a string. If set to <code>False</code> the representation will be left as a <code>Decimal</code> instance and the final representation will be determined by the renderer.</p>
|
|
<p>If unset, this will default to the same value as the <code>COERCE_DECIMAL_TO_STRING</code> setting, which is <code>True</code> unless set otherwise.</p>
|
|
<hr />
|
|
<h1 id="date-and-time-fields">Date and time fields</h1>
|
|
<h2 id="datetimefield">DateTimeField</h2>
|
|
<p>A date and time representation.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.DateTimeField</code>.</p>
|
|
<p><strong>Signature:</strong> <code>DateTimeField(format=None, input_formats=None)</code></p>
|
|
<ul>
|
|
<li><code>format</code> - A string representing the output format. If not specified, this defaults to the same value as the <code>DATETIME_FORMAT</code> settings key, which will be <code>'iso-8601'</code> unless set. Setting to a format string indicates that <code>to_representation</code> return values should be coerced to string output. Format strings are described below. Setting this value to <code>None</code> indicates that Python <code>datetime</code> objects should be returned by <code>to_representation</code>. In this case the datetime encoding will be determined by the renderer.</li>
|
|
<li><code>input_formats</code> - A list of strings representing the input formats which may be used to parse the date. If not specified, the <code>DATETIME_INPUT_FORMATS</code> setting will be used, which defaults to <code>['iso-8601']</code>.</li>
|
|
</ul>
|
|
<h4 id="datetimefield-format-strings"><code>DateTimeField</code> format strings.</h4>
|
|
<p>Format strings may either be <a href="http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior">Python strftime formats</a> which explicitly specify the format, or the special string <code>'iso-8601'</code>, which indicates that <a href="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a> style datetimes should be used. (eg <code>'2013-01-29T12:34:56.000000Z'</code>)</p>
|
|
<p>When a value of <code>None</code> is used for the format <code>datetime</code> objects will be returned by <code>to_representation</code> and the final output representation will determined by the renderer class.</p>
|
|
<p>In the case of JSON this means the default datetime representation uses the <a href="http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15">ECMA 262 date time string specification</a>. This is a subset of ISO 8601 which uses millisecond precision, and includes the 'Z' suffix for the UTC timezone, for example: <code>2013-01-29T12:34:56.123Z</code>.</p>
|
|
<h4 id="auto_now-and-auto_now_add-model-fields"><code>auto_now</code> and <code>auto_now_add</code> model fields.</h4>
|
|
<p>When using <code>ModelSerializer</code> or <code>HyperlinkedModelSerializer</code>, note that any model fields with <code>auto_now=True</code> or <code>auto_now_add=True</code> will use serializer fields that are <code>read_only=True</code> by default.</p>
|
|
<p>If you want to override this behavior, you'll need to declare the <code>DateTimeField</code> explicitly on the serializer. For example:</p>
|
|
<pre><code>class CommentSerializer(serializers.ModelSerializer):
|
|
created = serializers.DateTimeField()
|
|
|
|
class Meta:
|
|
model = Comment
|
|
</code></pre>
|
|
<h2 id="datefield">DateField</h2>
|
|
<p>A date representation.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.DateField</code></p>
|
|
<p><strong>Signature:</strong> <code>DateField(format=None, input_formats=None)</code></p>
|
|
<ul>
|
|
<li><code>format</code> - A string representing the output format. If not specified, this defaults to the same value as the <code>DATE_FORMAT</code> settings key, which will be <code>'iso-8601'</code> unless set. Setting to a format string indicates that <code>to_representation</code> return values should be coerced to string output. Format strings are described below. Setting this value to <code>None</code> indicates that Python <code>date</code> objects should be returned by <code>to_representation</code>. In this case the date encoding will be determined by the renderer.</li>
|
|
<li><code>input_formats</code> - A list of strings representing the input formats which may be used to parse the date. If not specified, the <code>DATE_INPUT_FORMATS</code> setting will be used, which defaults to <code>['iso-8601']</code>.</li>
|
|
</ul>
|
|
<h4 id="datefield-format-strings"><code>DateField</code> format strings</h4>
|
|
<p>Format strings may either be <a href="http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior">Python strftime formats</a> which explicitly specify the format, or the special string <code>'iso-8601'</code>, which indicates that <a href="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a> style dates should be used. (eg <code>'2013-01-29'</code>)</p>
|
|
<h2 id="timefield">TimeField</h2>
|
|
<p>A time representation.</p>
|
|
<p>Corresponds to <code>django.db.models.fields.TimeField</code></p>
|
|
<p><strong>Signature:</strong> <code>TimeField(format=None, input_formats=None)</code></p>
|
|
<ul>
|
|
<li><code>format</code> - A string representing the output format. If not specified, this defaults to the same value as the <code>TIME_FORMAT</code> settings key, which will be <code>'iso-8601'</code> unless set. Setting to a format string indicates that <code>to_representation</code> return values should be coerced to string output. Format strings are described below. Setting this value to <code>None</code> indicates that Python <code>time</code> objects should be returned by <code>to_representation</code>. In this case the time encoding will be determined by the renderer.</li>
|
|
<li><code>input_formats</code> - A list of strings representing the input formats which may be used to parse the date. If not specified, the <code>TIME_INPUT_FORMATS</code> setting will be used, which defaults to <code>['iso-8601']</code>.</li>
|
|
</ul>
|
|
<h4 id="timefield-format-strings"><code>TimeField</code> format strings</h4>
|
|
<p>Format strings may either be <a href="http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior">Python strftime formats</a> which explicitly specify the format, or the special string <code>'iso-8601'</code>, which indicates that <a href="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a> style times should be used. (eg <code>'12:34:56.000000'</code>)</p>
|
|
<hr />
|
|
<h1 id="choice-selection-fields">Choice selection fields</h1>
|
|
<h2 id="choicefield">ChoiceField</h2>
|
|
<p>A field that can accept a value out of a limited set of choices.</p>
|
|
<p>Used by <code>ModelSerializer</code> to automatically generate fields if the corresponding model field includes a <code>choices=…</code> argument.</p>
|
|
<p><strong>Signature:</strong> <code>ChoiceField(choices)</code></p>
|
|
<ul>
|
|
<li><code>choices</code> - A list of valid values, or a list of <code>(key, display_name)</code> tuples.</li>
|
|
<li><code>allow_blank</code> - If set to <code>True</code> then the empty string should be considered a valid value. If set to <code>False</code> then the empty string is considered invalid and will raise a validation error. Defaults to <code>False</code>.</li>
|
|
</ul>
|
|
<p>Both the <code>allow_blank</code> and <code>allow_null</code> are valid options on <code>ChoiceField</code>, although it is highly recommended that you only use one and not both. <code>allow_blank</code> should be preferred for textual choices, and <code>allow_null</code> should be preferred for numeric or other non-textual choices.</p>
|
|
<h2 id="multiplechoicefield">MultipleChoiceField</h2>
|
|
<p>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. <code>to_internal_representation</code> returns a <code>set</code> containing the selected values.</p>
|
|
<p><strong>Signature:</strong> <code>MultipleChoiceField(choices)</code></p>
|
|
<ul>
|
|
<li><code>choices</code> - A list of valid values, or a list of <code>(key, display_name)</code> tuples.</li>
|
|
<li><code>allow_blank</code> - If set to <code>True</code> then the empty string should be considered a valid value. If set to <code>False</code> then the empty string is considered invalid and will raise a validation error. Defaults to <code>False</code>.</li>
|
|
</ul>
|
|
<p>As with <code>ChoiceField</code>, both the <code>allow_blank</code> and <code>allow_null</code> options are valid, although it is highly recommended that you only use one and not both. <code>allow_blank</code> should be preferred for textual choices, and <code>allow_null</code> should be preferred for numeric or other non-textual choices.</p>
|
|
<hr />
|
|
<h1 id="file-upload-fields">File upload fields</h1>
|
|
<h4 id="parsers-and-file-uploads">Parsers and file uploads.</h4>
|
|
<p>The <code>FileField</code> and <code>ImageField</code> classes are only suitable for use with <code>MultiPartParser</code> or <code>FileUploadParser</code>. Most parsers, such as e.g. JSON don't support file uploads.
|
|
Django's regular <a href="https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FILE_UPLOAD_HANDLERS">FILE_UPLOAD_HANDLERS</a> are used for handling uploaded files.</p>
|
|
<h2 id="filefield">FileField</h2>
|
|
<p>A file representation. Performs Django's standard FileField validation.</p>
|
|
<p>Corresponds to <code>django.forms.fields.FileField</code>.</p>
|
|
<p><strong>Signature:</strong> <code>FileField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)</code></p>
|
|
<ul>
|
|
<li><code>max_length</code> - Designates the maximum length for the file name.</li>
|
|
<li><code>allow_empty_file</code> - Designates if empty files are allowed.</li>
|
|
<li><code>use_url</code> - If set to <code>True</code> then URL string values will be used for the output representation. If set to <code>False</code> then filename string values will be used for the output representation. Defaults to the value of the <code>UPLOADED_FILES_USE_URL</code> settings key, which is <code>True</code> unless set otherwise.</li>
|
|
</ul>
|
|
<h2 id="imagefield">ImageField</h2>
|
|
<p>An image representation. Validates the uploaded file content as matching a known image format.</p>
|
|
<p>Corresponds to <code>django.forms.fields.ImageField</code>.</p>
|
|
<p><strong>Signature:</strong> <code>ImageField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)</code></p>
|
|
<ul>
|
|
<li><code>max_length</code> - Designates the maximum length for the file name.</li>
|
|
<li><code>allow_empty_file</code> - Designates if empty files are allowed.</li>
|
|
<li><code>use_url</code> - If set to <code>True</code> then URL string values will be used for the output representation. If set to <code>False</code> then filename string values will be used for the output representation. Defaults to the value of the <code>UPLOADED_FILES_USE_URL</code> settings key, which is <code>True</code> unless set otherwise.</li>
|
|
</ul>
|
|
<p>Requires either the <code>Pillow</code> package or <code>PIL</code> package. The <code>Pillow</code> package is recommended, as <code>PIL</code> is no longer actively maintained.</p>
|
|
<hr />
|
|
<h1 id="composite-fields">Composite fields</h1>
|
|
<h2 id="listfield">ListField</h2>
|
|
<p>A field class that validates a list of objects.</p>
|
|
<p><strong>Signature</strong>: <code>ListField(child)</code></p>
|
|
<ul>
|
|
<li><code>child</code> - A field instance that should be used for validating the objects in the list.</li>
|
|
</ul>
|
|
<p>For example, to validate a list of integers you might use something like the following:</p>
|
|
<pre><code>scores = serializers.ListField(
|
|
child=serializers.IntegerField(min_value=0, max_value=100)
|
|
)
|
|
</code></pre>
|
|
<p>The <code>ListField</code> class also supports a declarative style that allows you to write reusable list field classes.</p>
|
|
<pre><code>class StringListField(serializers.ListField):
|
|
child = serializers.CharField()
|
|
</code></pre>
|
|
<p>We can now reuse our custom <code>StringListField</code> class throughout our application, without having to provide a <code>child</code> argument to it.</p>
|
|
<hr />
|
|
<h1 id="miscellaneous-fields">Miscellaneous fields</h1>
|
|
<h2 id="readonlyfield">ReadOnlyField</h2>
|
|
<p>A field class that simply returns the value of the field without modification.</p>
|
|
<p>This field is used by default with <code>ModelSerializer</code> when including field names that relate to an attribute rather than a model field.</p>
|
|
<p><strong>Signature</strong>: <code>ReadOnlyField()</code></p>
|
|
<p>For example, is <code>has_expired</code> was a property on the <code>Account</code> model, then the following serializer would automatically generate it as a <code>ReadOnlyField</code>:</p>
|
|
<pre><code>class AccountSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Account
|
|
fields = ('id', 'account_name', 'has_expired')
|
|
</code></pre>
|
|
<h2 id="hiddenfield">HiddenField</h2>
|
|
<p>A field class that does not take a value based on user input, but instead takes its value from a default value or callable.</p>
|
|
<p><strong>Signature</strong>: <code>HiddenField()</code></p>
|
|
<p>For example, to include a field that always provides the current time as part of the serializer validated data, you would use the following:</p>
|
|
<pre><code>modified = serializer.HiddenField(default=timezone.now)
|
|
</code></pre>
|
|
<p>The <code>HiddenField</code> 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.</p>
|
|
<p>For further examples on <code>HiddenField</code> see the <a href="../validators">validators</a> documentation.</p>
|
|
<h2 id="modelfield">ModelField</h2>
|
|
<p>A generic field that can be tied to any arbitrary model field. The <code>ModelField</code> 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.</p>
|
|
<p>This field is used by <code>ModelSerializer</code> to correspond to custom model field classes.</p>
|
|
<p><strong>Signature:</strong> <code>ModelField(model_field=<Django ModelField instance>)</code></p>
|
|
<p>The <code>ModelField</code> class is generally intended for internal use, but can be used by your API if needed. In order to properly instantiate a <code>ModelField</code>, it must be passed a field that is attached to an instantiated model. For example: <code>ModelField(model_field=MyModel()._meta.get_field('custom_field'))</code></p>
|
|
<h2 id="serializermethodfield">SerializerMethodField</h2>
|
|
<p>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.</p>
|
|
<p><strong>Signature</strong>: <code>SerializerMethodField(method_name=None)</code></p>
|
|
<ul>
|
|
<li><code>method-name</code> - The name of the method on the serializer to be called. If not included this defaults to <code>get_<field_name></code>.</li>
|
|
</ul>
|
|
<p>The serializer method referred to by the <code>method_name</code> argument should accept a single argument (in addition to <code>self</code>), which is the object being serialized. It should return whatever you want to be included in the serialized representation of the object. For example:</p>
|
|
<pre><code>from django.contrib.auth.models import User
|
|
from django.utils.timezone import now
|
|
from rest_framework import serializers
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
days_since_joined = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = User
|
|
|
|
def get_days_since_joined(self, obj):
|
|
return (now() - obj.date_joined).days
|
|
</code></pre>
|
|
<hr />
|
|
<h1 id="custom-fields">Custom fields</h1>
|
|
<p>If you want to create a custom field, you'll need to subclass <code>Field</code> and then override either one or both of the <code>.to_representation()</code> and <code>.to_internal_value()</code> 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, <code>date</code>/<code>time</code>/<code>datetime</code> or <code>None</code>. 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.</p>
|
|
<p>The <code>.to_representation()</code> method is called to convert the initial datatype into a primitive, serializable datatype.</p>
|
|
<p>The <code>to_internal_value()</code> method is called to restore a primitive datatype into its internal python representation. This method should raise a <code>serializer.ValidationError</code> if the data is invalid.</p>
|
|
<p>Note that the <code>WritableField</code> class that was present in version 2.x no longer exists. You should subclass <code>Field</code> and override <code>to_internal_value()</code> if the field supports data input.</p>
|
|
<h2 id="examples">Examples</h2>
|
|
<p>Let's look at an example of serializing a class that represents an RGB color value:</p>
|
|
<pre><code>class Color(object):
|
|
"""
|
|
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, blue
|
|
|
|
class ColorField(serializers.Field):
|
|
"""
|
|
Color objects are serialized into 'rgb(#, #, #)' notation.
|
|
"""
|
|
def to_representation(self, obj):
|
|
return "rgb(%d, %d, %d)" % (obj.red, obj.green, obj.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)
|
|
</code></pre>
|
|
<p>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 <code>.get_attribute()</code> and/or <code>.get_value()</code>.</p>
|
|
<p>As an example, let's create a field that can be used represent the class name of the object being serialized:</p>
|
|
<pre><code>class ClassNameField(serializers.Field):
|
|
def get_attribute(self, obj):
|
|
# We pass the object instance onto `to_representation`,
|
|
# not just the field attribute.
|
|
return obj
|
|
|
|
def to_representation(self, obj):
|
|
"""
|
|
Serialize the object's class name.
|
|
"""
|
|
return obj.__class__.__name__
|
|
</code></pre>
|
|
<h4 id="raising-validation-errors">Raising validation errors</h4>
|
|
<p>Our <code>ColorField</code> class above currently does not perform any data validation.
|
|
To indicate invalid data, we should raise a <code>serializers.ValidationError</code>, like so:</p>
|
|
<pre><code>def to_internal_value(self, data):
|
|
if not isinstance(data, six.text_type):
|
|
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)
|
|
</code></pre>
|
|
<p>The <code>.fail()</code> method is a shortcut for raising <code>ValidationError</code> that takes a message string from the <code>error_messages</code> dictionary. For example:</p>
|
|
<pre><code>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, six.text_type):
|
|
msg = 'Incorrect type. Expected a string, but got %s'
|
|
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)
|
|
</code></pre>
|
|
<p>This style keeps you error messages more cleanly separated from your code, and should be preferred.</p>
|
|
<h1 id="third-party-packages">Third party packages</h1>
|
|
<p>The following third party packages are also available.</p>
|
|
<h2 id="drf-compound-fields">DRF Compound Fields</h2>
|
|
<p>The <a href="http://drf-compound-fields.readthedocs.org">drf-compound-fields</a> package provides "compound" serializer fields, such as lists of simple values, which can be described by other fields rather than serializers with the <code>many=True</code> 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.</p>
|
|
<h2 id="drf-extra-fields">DRF Extra Fields</h2>
|
|
<p>The <a href="https://github.com/Hipo/drf-extra-fields">drf-extra-fields</a> package provides extra serializer fields for REST framework, including <code>Base64ImageField</code> and <code>PointField</code> classes.</p>
|
|
<h2 id="django-rest-framework-gis">django-rest-framework-gis</h2>
|
|
<p>The <a href="https://github.com/djangonauts/django-rest-framework-gis">django-rest-framework-gis</a> package provides geographic addons for django rest framework like a <code>GeometryField</code> field and a GeoJSON serializer.</p>
|
|
<h2 id="django-rest-framework-hstore">django-rest-framework-hstore</h2>
|
|
<p>The <a href="https://github.com/djangonauts/django-rest-framework-hstore">django-rest-framework-hstore</a> package provides an <code>HStoreField</code> to support <a href="https://github.com/djangonauts/django-hstore">django-hstore</a> <code>DictionaryField</code> model field.</p>
|
|
|
|
</div>
|
|
<!--/span-->
|
|
</div>
|
|
<!--/row-->
|
|
</div>
|
|
<!--/.fluid-container-->
|
|
</div>
|
|
<!--/.body content-->
|
|
<div id="push"></div>
|
|
</div>
|
|
<!--/.wrapper -->
|
|
|
|
<footer class="span12">
|
|
<p>Documentation built with <a href="http://www.mkdocs.org/">MkDocs</a>.</a>
|
|
</p>
|
|
</footer>
|
|
|
|
<!-- Le javascript
|
|
================================================== -->
|
|
<!-- Placed at the end of the document so the pages load faster -->
|
|
<script src="../../js/jquery-1.8.1-min.js"></script>
|
|
<script src="../../js/prettify-1.0.js"></script>
|
|
<script src="../../js/bootstrap-2.1.1-min.js"></script>
|
|
<script src="../../js/theme.js"></script>
|
|
|
|
<script>
|
|
//$('.side-nav').scrollspy()
|
|
var shiftWindow = function() {
|
|
scrollBy(0, -50)
|
|
};
|
|
if (location.hash) shiftWindow();
|
|
window.addEventListener("hashchange", shiftWindow);
|
|
|
|
$('.dropdown-menu').on('click touchstart', function(event) {
|
|
event.stopPropagation();
|
|
});
|
|
|
|
// Dynamically force sidenav to no higher than browser window
|
|
$('.side-nav').css('max-height', window.innerHeight - 130);
|
|
|
|
$(function() {
|
|
$(window).resize(function() {
|
|
$('.side-nav').css('max-height', window.innerHeight - 130);
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html> |