diff options
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/api-guide/fields.md | 22 | ||||
| -rwxr-xr-x | docs/api-guide/generic-views.md | 6 | ||||
| -rw-r--r-- | docs/api-guide/metadata.md | 103 | ||||
| -rw-r--r-- | docs/api-guide/renderers.md | 29 | ||||
| -rw-r--r-- | docs/api-guide/settings.md | 48 | ||||
| -rw-r--r-- | docs/api-guide/throttling.md | 2 | ||||
| -rw-r--r-- | docs/api-guide/validators.md | 183 | ||||
| -rw-r--r-- | docs/index.md | 4 | ||||
| -rw-r--r-- | docs/template.html | 1 | ||||
| -rw-r--r-- | docs/topics/2.4-announcement.md | 2 | ||||
| -rw-r--r-- | docs/topics/3.0-announcement.md | 816 | ||||
| -rw-r--r-- | docs/topics/contributing.md | 2 | ||||
| -rw-r--r-- | docs/topics/release-notes.md | 2 | ||||
| -rw-r--r-- | docs/topics/writable-nested-serializers.md | 2 | ||||
| -rw-r--r-- | docs/tutorial/1-serialization.md | 79 | ||||
| -rw-r--r-- | docs/tutorial/4-authentication-and-permissions.md | 14 | ||||
| -rw-r--r-- | docs/tutorial/quickstart.md | 8 | 
17 files changed, 1240 insertions, 83 deletions
| diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index bfbff2ad..292a51d8 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -274,7 +274,27 @@ Corresponds to `django.db.models.fields.FloatField`.  ## DecimalField -A decimal representation. +A decimal representation, represented in Python by a Decimal instance. + +Has two required arguments: + +- `max_digits` The maximum number of digits allowed in the number. Note that this number must be greater than or equal to decimal_places. + +- `decimal_places` The number of decimal places to store with the number. + +For example, 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) + +This field also takes an optional argument, `coerce_to_string`. If set to `True` the representation will be output as a string. If set to `False` the representation will be left as a `Decimal` instance and the final representation will be determined by the renderer. + +If unset, this will default to the same value as the `COERCE_DECIMAL_TO_STRING` setting, which is `True` unless set otherwise. + +**Signature:** `DecimalField(max_digits, decimal_places, coerce_to_string=None)`  Corresponds to `django.db.models.fields.DecimalField`. diff --git a/docs/api-guide/generic-views.md b/docs/api-guide/generic-views.md index b1c4e65a..49be0cae 100755 --- a/docs/api-guide/generic-views.md +++ b/docs/api-guide/generic-views.md @@ -19,8 +19,8 @@ Typically when using the generic views, you'll override the view, and set severa      from django.contrib.auth.models import User      from myapp.serializers import UserSerializer -	from rest_framework import generics -	from rest_framework.permissions import IsAdminUser +    from rest_framework import generics +    from rest_framework.permissions import IsAdminUser      class UserList(generics.ListCreateAPIView):          queryset = User.objects.all() @@ -212,8 +212,6 @@ Provides a `.list(request, *args, **kwargs)` method, that implements listing a q  If the queryset is populated, this returns a `200 OK` response, with a serialized representation of the queryset as the body of the response.  The response data may optionally be paginated. -If the queryset is empty this returns a `200 OK` response, unless the `.allow_empty` attribute on the view is set to `False`, in which case it will return a `404 Not Found`. -  ## CreateModelMixin  Provides a `.create(request, *args, **kwargs)` method, that implements creating and saving a new model instance. diff --git a/docs/api-guide/metadata.md b/docs/api-guide/metadata.md new file mode 100644 index 00000000..c3f036b7 --- /dev/null +++ b/docs/api-guide/metadata.md @@ -0,0 +1,103 @@ +<a class="github" href="metadata.py"></a> + +# Metadata + +> [The `OPTIONS`] method allows a client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval. +> +> — [RFC7231, Section 4.3.7.][cite] + +REST framework includes a configurable mechanism for determining how your API should respond to `OPTIONS` requests. This allows you to return API schema or other resource information. + +There are not currently any widely adopted conventions for exactly what style of response should be returned for HTTP `OPTIONS` requests, so we provide an ad-hoc style that returns some useful information. + +Here's an example response that demonstrates the information that is returned by default. + +    HTTP 200 OK +    Allow: GET, POST, HEAD, OPTIONS +    Content-Type: application/json + +    { +        "name": "To Do List", +        "description": "List existing 'To Do' items, or create a new item.", +        "renders": [ +            "application/json", +            "text/html" +        ], +        "parses": [ +            "application/json", +            "application/x-www-form-urlencoded", +            "multipart/form-data" +        ], +        "actions": { +            "POST": { +                "note": { +                    "type": "string", +                    "required": false, +                    "read_only": false, +                    "label": "title", +                    "max_length": 100 +                } +            } +        } +    } + +## Setting the metadata scheme + +You can set the metadata class globally using the `'DEFAULT_METADATA_CLASS'` settings key: + +    REST_FRAMEWORK = { +        'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata' +    } + +Or you can set the metadata class individually for a view: + +    class APIRoot(APIView): +        metadata_class = APIRootMetadata +         +        def get(self, request, format=None): +            return Response({ +                ... +            }) + +The REST framework package only includes a single metadata class implementation, named `SimpleMetadata`. If you want to use an alternative style you'll need to implement a custom metadata class. + +## Creating schema endpoints + +If you have specific requirements for creating schema endpoints that are accessed with regular `GET` requests, you might consider re-using the metadata API for doing so. + +For example, the following additional route could be used on a viewset to provide a linkable schema endpoint. + +    @list_route(methods=['GET']) +    def schema(self, request): +        meta = self.metadata_class() +        data = meta.determine_metadata(request, self) +        return Response(data) + +There are a couple of reasons that you might choose to take this approach, including that `OPTIONS` responses [are not cacheable][no-options]. + +--- + +# Custom metadata classes + +If you want to provide a custom metadata class you should override `BaseMetadata` and implement the `determine_metadata(self, request, view)` method. + +Useful things that you might want to do could include returning schema information, using a format such as [JSON schema][json-schema], or returning debug information to admin users. + +## Example + +The following class could be used to limit the information that is returned to `OPTIONS` requests. + +    class MinimalMetadata(BaseMetadata): +        """ +        Don't include field and other information for `OPTIONS` requests. +        Just return the name and description. +        """ +        def determine_metadata(self, request, view): +            return { +                'name': view.get_view_name(), +                'description': view.get_view_description() +            } + +[cite]: http://tools.ietf.org/html/rfc7231#section-4.3.7 +[no-options]: https://www.mnot.net/blog/2012/10/29/NO_OPTIONS +[json-schema]: http://json-schema.org/ diff --git a/docs/api-guide/renderers.md b/docs/api-guide/renderers.md index 20eed70d..db7436c2 100644 --- a/docs/api-guide/renderers.md +++ b/docs/api-guide/renderers.md @@ -74,37 +74,18 @@ If your API includes views that can serve both regular webpages and API response  Renders the request data into `JSON`, using utf-8 encoding. -Note that non-ascii characters will be rendered using JSON's `\uXXXX` character escape.  For example: +Note that the default style is to include unicode characters, and render the response using a compact style with no unnecessary whitespace: -    {"unicode black star": "\u2605"} +    {"unicode black star":"★","value":999}  The client may additionally include an `'indent'` media type parameter, in which case the returned `JSON` will be indented.  For example `Accept: application/json; indent=4`.      { -        "unicode black star": "\u2605" +        "unicode black star": "★", +        "value": 999      } -**.media_type**: `application/json` - -**.format**: `'.json'` - -**.charset**: `None` - -## UnicodeJSONRenderer - -Renders the request data into `JSON`, using utf-8 encoding. - -Note that non-ascii characters will not be character escaped.  For example: - -    {"unicode black star": "★"} - -The client may additionally include an `'indent'` media type parameter, in which case the returned `JSON` will be indented.  For example `Accept: application/json; indent=4`. - -    { -        "unicode black star": "★" -    } - -Both the `JSONRenderer` and `UnicodeJSONRenderer` styles conform to [RFC 4627][rfc4627], and are syntactically valid JSON. +The default JSON encoding style can be altered using the `UNICODE_JSON` and `COMPACT_JSON` settings keys.  **.media_type**: `application/json` diff --git a/docs/api-guide/settings.md b/docs/api-guide/settings.md index 27a09163..6a855c92 100644 --- a/docs/api-guide/settings.md +++ b/docs/api-guide/settings.md @@ -265,7 +265,7 @@ A format string that should be used by default for rendering the output of `Date  May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string. -Default: `None` +Default: `'iso-8601'`  #### DATETIME_INPUT_FORMATS @@ -281,7 +281,7 @@ A format string that should be used by default for rendering the output of `Date  May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string. -Default: `None` +Default: `'iso-8601'`  #### DATE_INPUT_FORMATS @@ -297,7 +297,7 @@ A format string that should be used by default for rendering the output of `Time  May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string. -Default: `None` +Default: `'iso-8601'`  #### TIME_INPUT_FORMATS @@ -309,6 +309,46 @@ Default: `['iso-8601']`  --- +## Encodings + +#### UNICODE_JSON + +When set to `True`, JSON responses will allow unicode characters in responses. For example: + +    {"unicode black star":"★"} + +When set to `False`, JSON responses will escape non-ascii characters, like so: + +    {"unicode black star":"\u2605"} + +Both styles conform to [RFC 4627][rfc4627], and are syntactically valid JSON. The unicode style is prefered as being more user-friendly when inspecting API responses. + +Default: `True` + +#### COMPACT_JSON + +When set to `True`, JSON responses will return compact representations, with no spacing after `':'` and `','` characters. For example: + +    {"is_admin":false,"email":"jane@example"} + +When set to `False`, JSON responses will return slightly more verbose representations, like so: + +    {"is_admin": false, "email": "jane@example"} + +The default style is to return minified responses, in line with [Heroku's API design guidelines][heroku-minified-json]. + +Default: `True` + +#### COERCE_DECIMAL_TO_STRING + +When returning decimal objects in API representations that do not support a native decimal type, it is normally best to return the value as a string. This avoids the loss of precision that occurs with binary floating point implementations. + +When set to `True`, the serializer `DecimalField` class will return strings instead of `Decimal` objects. When set to `False`, serializers will return `Decimal` objects, which the default JSON encoder will return as floats. + +Default: `True` + +--- +  ## View names and descriptions  **The following settings are used to generate the view names and descriptions, as used in responses to `OPTIONS` requests, and as used in the browsable API.** @@ -378,4 +418,6 @@ An integer of 0 or more, that may be used to specify the number of application p  Default: `None`  [cite]: http://www.python.org/dev/peps/pep-0020/ +[rfc4627]: http://www.ietf.org/rfc/rfc4627.txt +[heroku-minified-json]: https://github.com/interagent/http-api-design#keep-json-minified-in-all-responses  [strftime]: http://docs.python.org/2/library/time.html#time.strftime diff --git a/docs/api-guide/throttling.md b/docs/api-guide/throttling.md index 832304f1..16a7457b 100644 --- a/docs/api-guide/throttling.md +++ b/docs/api-guide/throttling.md @@ -178,6 +178,8 @@ To create a custom throttle, override `BaseThrottle` and implement `.allow_reque  Optionally you may also override the `.wait()` method.  If implemented, `.wait()` should return a recommended number of seconds to wait before attempting the next request, or `None`.  The `.wait()` method will only be called if `.allow_request()` has previously returned `False`. +If the `.wait()` method is implemented and the request is throttled, then a `Retry-After` header will be included in the response. +  ## Example  The following is an example of a rate throttle, that will randomly throttle 1 in every 10 requests. diff --git a/docs/api-guide/validators.md b/docs/api-guide/validators.md new file mode 100644 index 00000000..6a0ef4ff --- /dev/null +++ b/docs/api-guide/validators.md @@ -0,0 +1,183 @@ +<a class="github" href="validators.py"></a> + +# Validators + +> Validators can be useful for re-using validation logic between different types of fields. +> +> — [Django documentation][cite] + +Most of the time you're dealing with validation in REST framework you'll simply be relying on the default field validation, or writing explicit validation methods on serializer or field classes. + +However, sometimes you'll want to place your validation logic into reusable components, so that it can easily be reused throughout your codebase. This can be achieved by using validator functions and validator classes. + +## Validation in REST framework + +Validation in Django REST framework serializers is handled a little differently to how validation works in Django's `ModelForm` class. + +With `ModelForm` the validation is performed partially on the form, and partially on the model instance. With REST framework the validation is performed entirely on the serializer class. This is advantageous for the following reasons: + +* It introduces a proper separation of concerns, making your code behaviour more obvious. +* It is easy to switch between using shortcut `ModelSerializer` classes and using  explicit `Serializer` classes. Any validation behaviour being used for `ModelSerializer` is simple to replicate. +* Printing the `repr` of a serializer instance will show you exactly what validation rules it applies. There's no extra hidden validation behaviour being called on the model instance. + +When you're using `ModelSerializer` all of this is handled automatically for you. If you want to drop down to using a `Serializer` classes instead, then you need to define the validation rules explicitly. + +#### Example + +As an example of how REST framework uses explicit validation, we'll take a simple model class that has a field with a uniqueness constraint. + +    class CustomerReportRecord(models.Model): +        time_raised = models.DateTimeField(default=timezone.now, editable=False)  +        reference = models.CharField(unique=True, max_length=20) +        description = models.TextField() + +Here's a basic `ModelSerializer` that we can use for creating or updating instances of `CustomerReportRecord`: + +    class CustomerReportSerializer(serializers.ModelSerializer): +        class Meta: +            model = CustomerReportRecord + +If we open up the Django shell using `manage.py shell` we can now  + +    >>> from project.example.serializers import CustomerReportSerializer +    >>> serializer = CustomerReportSerializer() +    >>> print(repr(serializer)) +    CustomerReportSerializer(): +        id = IntegerField(label='ID', read_only=True) +        time_raised = DateTimeField(read_only=True) +        reference = CharField(max_length=20, validators=[<UniqueValidator(queryset=CustomerReportRecord.objects.all())>]) +        description = CharField(style={'type': 'textarea'}) + +The interesting bit here is the `reference` field. We can see that the uniqueness constraint is being explicitly enforced by a validator on the serializer field. + +Because of this more explicit style REST framework includes a few validator classes that are not available in core Django. These classes are detailed below. + +--- + +## UniqueValidator + +This validator can be used to enforce the `unique=True` constraint on model fields. +It takes a single required argument, and an optional `messages` argument: + +* `queryset` *required* - This is the queryset against which uniqueness should be enforced. +* `message` - The error message that should be used when validation fails. + +This validator should be applied to *serializer fields*, like so: + +    slug = SlugField( +        max_length=100, +        validators=[UniqueValidator(queryset=BlogPost.objects.all())] +    ) + +## UniqueTogetherValidator + +This validator can be used to enforce `unique_together` constraints on model instances. +It has two required arguments, and a single optional `messages` argument: + +* `queryset` *required* - This is the queryset against which uniqueness should be enforced. +* `fields` *required* - A list or tuple of field names which should make a unique set. These must exist as fields on the serializer class. +* `message` - The error message that should be used when validation fails. + +The validator should be applied to *serializer classes*, like so: + +    class ExampleSerializer(serializers.Serializer): +        # ... +        class Meta: +            # ToDo items belong to a parent list, and have an ordering defined +            # by the 'position' field. No two items in a given list may share +            # the same position. +            validators = [ +                UniqueTogetherValidator( +                    queryset=ToDoItem.objects.all(), +                    fields=('list', 'position') +                ) +            ] + +## UniqueForDateValidator + +## UniqueForMonthValidator + +## UniqueForYearValidator + +These validators can be used to enforce the `unique_for_date`, `unique_for_month` and `unique_for_year` constraints on model instances. They take the following arguments: + +* `queryset` *required* - This is the queryset against which uniqueness should be enforced. +* `field` *required* - A field name against which uniqueness in the given date range will be validated. This must exist as a field on the serializer class. +* `date_field` *required* - A field name which will be used to determine date range for the uniqueness constrain. This must exist as a field on the serializer class. +* `message` - The error message that should be used when validation fails. + +The validator should be applied to *serializer classes*, like so: + +    class ExampleSerializer(serializers.Serializer): +        # ... +        class Meta: +            # Blog posts should have a slug that is unique for the current year. +            validators = [ +                UniqueForYearValidator( +                    queryset=BlogPostItem.objects.all(), +                    field='slug', +                    date_field='published' +                ) +            ] + +The date field that is used for the validation is always required to be present on the serializer class. You can't simply rely on a model class `default=...`, because the value being used for the default wouldn't be generated until after the validation has run. + +There are a couple of styles you may want to use for this depending on how you want your API to behave. If you're using `ModelSerializer` you'll probably simply rely on the defaults that REST framework generates for you, but if you are using `Serializer` or simply want more explicit control, use on of the styles demonstrated below. + +#### Using with a writable date field. + +If you want the date field to be writable the only thing worth noting is that you should ensure that it is always available in the input data, either by setting a `default` argument, or by setting `required=True`. + +    published = serializers.DateTimeField(required=True) + +#### Using with a read-only date field. + +If you want the date field to be visible, but not editable by the user, then set `read_only=True` and additionally set a `default=...` argument. + +    published = serializers.DateTimeField(read_only=True, default=timezone.now) + +The field will not be writable to the user, but the default value will still be passed through to the `validated_data`. + +#### Using with a hidden date field. + +If you want the date field to be entirely hidden from the user, then use `HiddenField`. This field type does not accept user input, but instead always returns it's default value to the `validated_data` in the serializer. + +    published = serializers.HiddenField(default=timezone.now) + +--- + +# Writing custom validators + +You can use any of Django's existing validators, or write your own custom validators. + +## Function based + +A validator may be any callable that raises a `serializers.ValidationError` on failure. + +    def even_number(value): +        if value % 2 != 0: +            raise serializers.ValidationError('This field must be an even number.')  + +## Class based + +To write a class based validator, use the `__call__` method. Class based validators are useful as they allow you to parameterize and reuse behavior. + +    class MultipleOf: +        def __init__(self, base): +            self.base = base +  +        def __call__(self, value): +            if value % self.base != 0 +                message = 'This field must be a multiple of %d.' % self.base +                raise serializers.ValidationError(message) + +#### Using `set_context()` + +In some advanced cases you might want a validator to be passed the serializer field it is being used with as additional context. You can do so by declaring a `set_context` method on a class based validator. + +    def set_context(self, serializer_field): +        # Determine if this is an update or a create operation. +        # In `__call__` we can then use that information to modify the validation behavior. +        self.is_update = serializer_field.parent.instance is not None + +[cite]: https://docs.djangoproject.com/en/dev/ref/validators/ diff --git a/docs/index.md b/docs/index.md index b18b71d2..5b31cc83 100644 --- a/docs/index.md +++ b/docs/index.md @@ -50,7 +50,7 @@ Some reasons you might want to use REST framework:  REST framework requires the following:  * Python (2.6.5+, 2.7, 3.2, 3.3, 3.4) -* Django (1.4.2+, 1.5, 1.6, 1.7) +* Django (1.4.11+, 1.5.5+, 1.6, 1.7)  The following packages are optional: @@ -173,6 +173,7 @@ The API guide is your complete reference manual to all the functionality provide  * [Serializers][serializers]  * [Serializer fields][fields]  * [Serializer relations][relations] +* [Validators][validators]  * [Authentication][authentication]  * [Permissions][permissions]  * [Throttling][throttling] @@ -294,6 +295,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  [serializers]: api-guide/serializers.md  [fields]: api-guide/fields.md  [relations]: api-guide/relations.md +[validation]: api-guide/validation.md  [authentication]: api-guide/authentication.md  [permissions]: api-guide/permissions.md  [throttling]: api-guide/throttling.md diff --git a/docs/template.html b/docs/template.html index bb3ae221..f36cffc6 100644 --- a/docs/template.html +++ b/docs/template.html @@ -95,6 +95,7 @@ a.fusion-poweredby {                    <li><a href="{{ base_url }}/api-guide/serializers{{ suffix }}">Serializers</a></li>                    <li><a href="{{ base_url }}/api-guide/fields{{ suffix }}">Serializer fields</a></li>                    <li><a href="{{ base_url }}/api-guide/relations{{ suffix }}">Serializer relations</a></li> +                  <li><a href="{{ base_url }}/api-guide/validators{{ suffix }}">Validators</a></li>                    <li><a href="{{ base_url }}/api-guide/authentication{{ suffix }}">Authentication</a></li>                    <li><a href="{{ base_url }}/api-guide/permissions{{ suffix }}">Permissions</a></li>                    <li><a href="{{ base_url }}/api-guide/throttling{{ suffix }}">Throttling</a></li> diff --git a/docs/topics/2.4-announcement.md b/docs/topics/2.4-announcement.md index 8e4f3bb2..f38c743c 100644 --- a/docs/topics/2.4-announcement.md +++ b/docs/topics/2.4-announcement.md @@ -23,7 +23,7 @@ The documentation has previously stated that usage of the more explicit style is  Doing so will mean that there are cases of API code where you'll now need to include a serializer class where you previously were just using the `.model` shortcut. However we firmly believe that it is the right trade-off to make. -Removing the shortcut takes away an unneccessary layer of abstraction, and makes your codebase more explicit without any significant extra complexity. It also results in better consistency, as there's now only one way to set the serializer class and queryset attributes for the view, instead of two. +Removing the shortcut takes away an unnecessary layer of abstraction, and makes your codebase more explicit without any significant extra complexity. It also results in better consistency, as there's now only one way to set the serializer class and queryset attributes for the view, instead of two.  The `DEFAULT_MODEL_SERIALIZER_CLASS` API setting is now also deprecated. diff --git a/docs/topics/3.0-announcement.md b/docs/topics/3.0-announcement.md new file mode 100644 index 00000000..9aeb5df6 --- /dev/null +++ b/docs/topics/3.0-announcement.md @@ -0,0 +1,816 @@ +## Pre-release notes: + +The 3.0 release is now ready for some tentative testing and upgrades for super keen early adopters. You can install the development version directly from GitHub like so: + +    pip install https://github.com/tomchristie/django-rest-framework/archive/version-3.0.zip + +See the [Version 3.0 GitHub issue](https://github.com/tomchristie/django-rest-framework/pull/1800) for more details on remaining work. + +The most notable outstanding issues still to be resolved on the `version-3.0` branch are as follows: + +* Finish forms support for serializers and in the browsable API. +* Optimisations for serializing primary keys. +* Refine style of validation errors in some cases, such as validation errors in `ListField`. + +**Your feedback on the upgrade process and 3.0 changes is hugely important!** + +Please do get in touch via twitter, IRC, a GitHub ticket, or the discussion group. + +--- + +# REST framework 3.0 + +The 3.0 release of Django REST framework is the result of almost four years of iteration and refinement. It comprehensively addresses some of the previous remaining design issues in serializers, fields and the generic views. + +This release is incremental in nature. There *are* some breaking API changes, and upgrading *will* require you to read the release notes carefully, but the migration path should otherwise be relatively straightforward. + +The difference in quality of the REST framework API and implementation should make writing, maintaining and debugging your application far easier. + +## New features + +Notable features of this new release include: + +* Printable representations on serializers that allow you to inspect exactly what fields are present on the instance. +* Simple model serializers that are vastly easier to understand and debug, and that make it easy to switch between the implicit `ModelSerializer` class and the explicit `Serializer` class. +* A new `BaseSerializer` class, making it easier to write serializers for alternative storage backends, or to completely customize your serialization and validation logic. +* A cleaner fields API plus new `ListField` and `MultipleChoiceField` classes. +* Super simple default implementations for the generic views. +* Support for overriding how validation errors are handled by your API. +* A metadata API that allows you to customize how `OPTIONS` requests are handled by your API. +* A more compact JSON output with unicode style encoding turned on by default. + +Below is an in-depth guide to the API changes and migration notes for 3.0. + +--- + +## Request objects + +#### The `.data` and `.query_params` properties. + +The usage of `request.DATA` and `request.FILES` is now pending deprecation in favor of a single `request.data` attribute that contains *all* the parsed data. + +Having separate attributes is reasonable for web applications that only ever parse url-encoded or multipart requests, but makes less sense for the general-purpose request parsing that REST framework supports. + +You may now pass all the request data to a serializer class in a single argument: + +    # Do this... +    ExampleSerializer(data=request.data) + +Instead of passing the files argument separately: + +    # Don't do this... +    ExampleSerializer(data=request.DATA, files=request.FILES) + + +The usage of `request.QUERY_PARAMS` is now pending deprecation in favor of the lowercased `request.query_params`. + +## Serializers + +#### Single-step object creation. + +Previously the serializers used a two-step object creation, as follows: + +1. Validating the data would create an object instance. This instance would be available as `serializer.object`. +2. Calling `serializer.save()` would then save the object instance to the database. + +This style is in-line with how the `ModelForm` class works in Django, but is problematic for a number of reasons: + +* Some data, such as many-to-many relationships, cannot be added to the object instance until after it has been saved. This type of data needed to be hidden in some undocumented state on the object instance, or kept as state on the serializer instance so that it could be used when `.save()` is called. +* Instantiating model instances directly means that you cannot use model manager classes for instance creation, eg `ExampleModel.objects.create(...)`. Manager classes are an excellent layer at which to enforce business logic and application-level data constraints. +* The two step process makes it unclear where to put deserialization logic. For example, should extra attributes such as the current user get added to the instance during object creation or during object save? + +We now use single-step object creation, like so: + +1. Validating the data makes the cleaned data available as `serializer.validated_data`. +2. Calling `serializer.save()` then saves and returns the new object instance. + +The resulting API changes are further detailed below. + +#### The `.create()` and `.update()` methods. + +The `.restore_object()` method is now replaced with two separate methods, `.create()` and `.update()`. + +When using the `.create()` and `.update()` methods you should both create *and save* the object instance. This is in contrast to the previous `.restore_object()` behavior that would instantiate the object but not save it. + +The following example from the tutorial previously used `restore_object()` to handle both creating and updating object instances. + +    def restore_object(self, attrs, instance=None): +        if instance: +            # Update existing instance +            instance.title = attrs.get('title', instance.title) +            instance.code = attrs.get('code', instance.code) +            instance.linenos = attrs.get('linenos', instance.linenos) +            instance.language = attrs.get('language', instance.language) +            instance.style = attrs.get('style', instance.style) +            return instance + +        # Create new instance +        return Snippet(**attrs) + +This would now be split out into two separate methods. + +    def update(self, instance, validated_data): +        instance.title = validated_data.get('title', instance.title) +        instance.code = validated_data.get('code', instance.code) +        instance.linenos = validated_data.get('linenos', instance.linenos) +        instance.language = validated_data.get('language', instance.language) +        instance.style = validated_data.get('style', instance.style) +        instance.save() +        return instance + +	def create(self, validated_data): +        return Snippet.objects.create(**validated_data) + +Note that these methods should return the newly created object instance. + +#### Use `.validated_data` instead of `.object`. + +You must now use the `.validated_data` attribute if you need to inspect the data before saving, rather than using the `.object` attribute, which no longer exists. + +For example the following code *is no longer valid*: + +    if serializer.is_valid(): +        name = serializer.object.name  # Inspect validated field data. +        logging.info('Creating ticket "%s"' % name) +        serializer.object.user = request.user  # Include the user when saving. +        serializer.save() + +Instead of using `.object` to inspect a partially constructed instance, you would now use `.validated_data` to inspect the cleaned incoming values. Also you can't set extra attributes on the instance directly, but instead pass them to the `.save()` method as keyword arguments. + +The corresponding code would now look like this: + +    if serializer.is_valid(): +        name = serializer.validated_data['name']  # Inspect validated field data. +        logging.info('Creating ticket "%s"' % name) +        serializer.save(user=request.user)  # Include the user when saving. + +#### Using `serializers.ValidationError`. + +Previously `serializers.ValidationError` error was simply a synonym for `django.core.exceptions.ValidationError`. This has now been altered so that it inherits from the standard `APIException` base class. + +The reason behind this is that Django's `ValidationError` class is intended for use with HTML forms and its API makes using it slightly awkward with nested validation errors that can occur in serializers. + +For most users this change shouldn't require any updates to your codebase, but it is worth ensuring that whenever raising validation errors you are always using the `serializers.ValidationError` exception class, and not Django's built-in exception. + +We strongly recommend that you use the namespaced import style of `import serializers` and not `from serializers import ValidationError` in order to avoid any potential confusion. + +#### Change to `validate_<field_name>`. + +The `validate_<field_name>` method hooks that can be attached to serializer classes change their signature slightly and return type. Previously these would take a dictionary of all incoming data, and a key representing the field name, and would return a dictionary including the validated data for that field: + +    def validate_score(self, attrs, source): +        if attrs[score] % 10 != 0: +            raise serializers.ValidationError('This field should be a multiple of ten.') +        return attrs + +This is now simplified slightly, and the method hooks simply take the value to be validated, and return the validated value. + +    def validate_score(self, value): +        if value % 10 != 0: +            raise serializers.ValidationError('This field should be a multiple of ten.') +        return value + +Any ad-hoc validation that applies to more than one field should go in the `.validate(self, attrs)` method as usual. + +Because `.validate_<field_name>` would previously accept the complete dictionary of attributes, it could be used to validate a field depending on the input in another field. Now if you need to do this you should use `.validate()` instead. + +You can either return `non_field_errors` from the validate method by raising a simple `ValidationError` + +    def validate(self, attrs): +        # serializer.errors == {'non_field_errors': ['A non field error']} +        raise serailizers.ValidationError('A non field error') + +Alternatively if you want the errors to be against a specific field, use a dictionary of when instantiating the `ValidationError`, like so: + +    def validate(self, attrs): +        # serializer.errors == {'my_field': ['A field error']} +        raise serailizers.ValidationError({'my_field': 'A field error'}) + +This ensures you can still write validation that compares all the input fields, but that marks the error against a particular field. + +#### Limitations of ModelSerializer validation. + +This change also means that we no longer use the `.full_clean()` method on model instances, but instead perform all validation explicitly on the serializer. This gives a cleaner separation, and ensures that there's no automatic validation behavior on `ModelSerializer` classes that can't also be easily replicated on regular `Serializer` classes. + +This change comes with the following limitations: + +* The model `.clean()` method will not be called as part of serializer validation. Use the serializer `.validate()` method to perform a final validation step on incoming data where required. +* The `.unique_for_date`, `.unique_for_month` and `.unique_for_year` options on model fields are not automatically validated. Again, you'll need to handle these explicitly on the serializer if required. + +#### Writable nested serialization. + +REST framework 2.x attempted to automatically support writable nested serialization, but the behavior was complex and non-obvious. Attempting to automatically handle these case is problematic: + +* There can be complex dependencies involved in order of saving multiple related model instances. +* It's unclear what behavior the user should expect when related models are passed `None` data. +* It's unclear how the user should expect to-many relationships to handle updates, creations and deletions of multiple records. + +Using the `depth` option on `ModelSerializer` will now create **read-only nested serializers** by default. + +If you try to use a writable nested serializer without writing a custom `create()` and/or `update()` method you'll see an assertion error when you attempt to save the serializer. For example: + +    >>> class ProfileSerializer(serializers.ModelSerializer): +    >>>     class Meta: +    >>>         model = Profile +    >>>         fields = ('address', 'phone') +    >>> +    >>> class UserSerializer(serializers.ModelSerializer): +    >>>     profile = ProfileSerializer() +    >>>     class Meta: +    >>>         model = User +    >>>         fields = ('username', 'email', 'profile') +    >>> +    >>> data = { +    >>>     'username': 'lizzy', +    >>>     'email': 'lizzy@example.com', +    >>>     'profile': {'address': '123 Acacia Avenue', 'phone': '01273 100200'} +    >>> } +    >>> +    >>> serializer = UserSerializer(data=data) +    >>> serializer.save() +    AssertionError: The `.create()` method does not suport nested writable fields by default. Write an explicit `.create()` method for serializer `UserSerializer`, or set `read_only=True` on nested serializer fields. + +To use writable nested serialization you'll want to declare a nested field on the serializer class, and write the `create()` and/or `update()` methods explicitly. + +    class UserSerializer(serializers.ModelSerializer): +        profile = ProfileSerializer() + +        class Meta: +            model = User +            fields = ('username', 'email', 'profile') + +        def create(self, validated_data): +            profile_data = validated_data.pop['profile'] +            user = User.objects.create(**validated_data) +            Profile.objects.create(user=user, **profile_data) +            return user + +The single-step object creation makes this far simpler and more obvious than the previous `.restore_object()` behavior. + +#### Printable serializer representations. + +Serializer instances now support a printable representation that allows you to inspect the fields present on the instance. + +For instance, given the following example model: + +    class LocationRating(models.Model): +        location = models.CharField(max_length=100) +        rating = models.IntegerField() +        created_by = models.ForeignKey(User) + +Let's create a simple `ModelSerializer` class corresponding to the `LocationRating` model. + +    class LocationRatingSerializer(serializer.ModelSerializer): +        class Meta: +            model = LocationRating + +We can now inspect the serializer representation in the Django shell, using `python manage.py shell`... + +    >>> serializer = LocationRatingSerializer() +    >>> print(serializer)  # Or use `print serializer` in Python 2.x +    LocationRatingSerializer(): +        id = IntegerField(label='ID', read_only=True) +        location = CharField(max_length=100) +        rating = IntegerField() +        created_by = PrimaryKeyRelatedField(queryset=User.objects.all()) + +#### The `extra_kwargs` option. + +The `write_only_fields` option on `ModelSerializer` has been moved to `PendingDeprecation` and replaced with a more generic `extra_kwargs`. + +    class MySerializer(serializer.ModelSerializer): +        class Meta: +            model = MyModel +            fields = ('id', 'email', 'notes', 'is_admin') +            extra_kwargs = { +            	'is_admin': {'write_only': True} +            } + +Alternatively, specify the field explicitly on the serializer class: + +    class MySerializer(serializer.ModelSerializer): +        is_admin = serializers.BooleanField(write_only=True) + +        class Meta: +            model = MyModel +            fields = ('id', 'email', 'notes', 'is_admin') + +The `read_only_fields` option remains as a convenient shortcut for the more common case.  + +#### Changes to `HyperlinkedModelSerializer`. + +The `view_name` and `lookup_field` options have been moved to `PendingDeprecation`. They are no longer required, as you can use the `extra_kwargs` argument instead: + +    class MySerializer(serializer.HyperlinkedModelSerializer): +        class Meta: +            model = MyModel +            fields = ('url', 'email', 'notes', 'is_admin') +            extra_kwargs = { +            	'url': {'lookup_field': 'uuid'} +            } + +Alternatively, specify the field explicitly on the serializer class: + +    class MySerializer(serializer.HyperlinkedModelSerializer): +        url = serializers.HyperlinkedIdentityField( +            view_name='mymodel-detail', +            lookup_field='uuid' +        ) + +        class Meta: +            model = MyModel +            fields = ('url', 'email', 'notes', 'is_admin') + +#### Fields for model methods and properties. + +With `ModelSerilizer` you can now specify field names in the `fields` option that refer to model methods or properties. For example, suppose you have the following model: + +    class Invitation(models.Model): +        created = models.DateTimeField() +        to_email = models.EmailField() +        message = models.CharField(max_length=1000) + +		def expiry_date(self): +		    return self.created + datetime.timedelta(days=30) + +You can include `expiry_date` as a field option on a `ModelSerializer` class. + +    class InvitationSerializer(serializers.ModelSerializer): +        class Meta: +            model = Invitation +            fields = ('to_email', 'message', 'expiry_date') + +These fields will be mapped to `serializers.ReadOnlyField()` instances. + +    >>> serializer = InvitationSerializer() +    >>> print repr(serializer) +    InvitationSerializer(): +        to_email = EmailField(max_length=75) +        message = CharField(max_length=1000) +        expiry_date = ReadOnlyField() + +#### The `ListSerializer` class. + +The `ListSerializer` class has now been added, and allows you to create base serializer classes for only accepting multiple inputs. + +    class MultipleUserSerializer(ListSerializer): +        child = UserSerializer() + +You can also still use the `many=True` argument to serializer classes. It's worth noting that `many=True` argument transparently creates a `ListSerializer` instance, allowing the validation logic for list and non-list data to be cleanly separated in the REST framework codebase.  + +See also the new `ListField` class, which validates input in the same way, but does not include the serializer interfaces of `.is_valid()`, `.data`, `.save()` and so on. + +#### The `BaseSerializer` class. + +REST framework now includes a simple `BaseSerializer` class that can be used to easily support alternative serialization and deserialization styles. + +This class implements the same basic API as the `Serializer` class: + +* `.data` - Returns the outgoing primitive representation. +* `.is_valid()` - Deserializes and validates incoming data. +* `.validated_data` - Returns the validated incoming data. +* `.errors` - Returns an errors during validation. +* `.save()` - Persists the validated data into an object instance. + +There are four mathods that can be overriding, depending on what functionality you want the serializer class to support: + +* `.to_representation()` - Override this to support serialization, for read operations. +* `.to_internal_value()` - Override this to support deserialization, for write operations. +* `.create()` and `.update()` - Overide either or both of these to support saving instances. + +##### Read-only `BaseSerializer` classes. + +To implement a read-only serializer using the `BaseSerializer` class, we just need to override the `.to_representation()` method. Let's take a look at an example using a simple Django model: + +    class HighScore(models.Model): +        created = models.DateTimeField(auto_now_add=True) +        player_name = models.CharField(max_length=10) +        score = models.IntegerField() + +It's simple to create a read-only serializer for converting `HighScore` instances into primitive data types. + +    class HighScoreSerializer(serializers.BaseSerializer): +        def to_representation(self, obj): +            return { +                'score': obj.score, +                'player_name': obj.player_name +            } + +We can now use this class to serialize single `HighScore` instances: + +    @api_view(['GET']) +    def high_score(request, pk): +        instance = HighScore.objects.get(pk=pk) +        serializer = HighScoreSerializer(instance) +	    return Response(serializer.data) + +Or use it to serialize multiple instances: + +    @api_view(['GET']) +    def all_high_scores(request): +        queryset = HighScore.objects.order_by('-score') +        serializer = HighScoreSerializer(queryset, many=True) +	    return Response(serializer.data) + +##### Read-write `BaseSerializer` classes. + +To create a read-write serializer we first need to implement a `.to_internal_value()` method. This method returns the validated values that will be used to construct the object instance, and may raise a `ValidationError` if the supplied data is in an incorrect format. + +Once you've implemented `.to_internal_value()`, the basic validation API will be available on the serializer, and you will be able to use `.is_valid()`, `.validated_data` and `.errors`. + +If you want to also support `.save()` you'll need to also implement either or both of the `.create()` and `.update()` methods. + +Here's a complete example of our previous `HighScoreSerializer`, that's been updated to support both read and write operations. + +    class HighScoreSerializer(serializers.BaseSerializer): +        def to_internal_value(self, data): +            score = data.get('score') +            player_name = data.get('player_name') +             +            # Perform the data validation. +            if not score: +                raise ValidationError({ +                    'score': 'This field is required.' +                }) +            if not player_name: +                raise ValidationError({ +                    'player_name': 'This field is required.' +                }) +            if len(player_name) > 10: +                raise ValidationError({ +                    'player_name': 'May not be more than 10 characters.' +                }) +  +			# Return the validated values. This will be available as +			# the `.validated_data` property. +            return { +                'score': int(score), +                'player_name': player_name +            } + +        def to_representation(self, obj): +            return { +                'score': obj.score, +                'player_name': obj.player_name +            } +         +        def create(self, validated_data): +            return HighScore.objects.create(**validated_data) + +#### Creating new generic serializers with `BaseSerializer`. + +The `BaseSerializer` class is also useful if you want to implement new generic serializer classes for dealing with particular serialization styles, or for integrating with alternative storage backends. + +The following class is an example of a generic serializer that can handle coercing aribitrary objects into primitive representations.  + +    class ObjectSerializer(serializers.BaseSerializer): +        """ +        A read-only serializer that coerces arbitrary complex objects +        into primitive representations. +        """ +        def to_representation(self, obj): +            for attribute_name in dir(obj): +                attribute = getattr(obj, attribute_name) +                if attribute_name('_'): +                    # Ignore private attributes. +                    pass +                elif hasattr(attribute, '__call__'): +                    # Ignore methods and other callables. +                    pass +                elif isinstance(attribute, (str, int, bool, float, type(None))): +                    # Primitive types can be passed through unmodified. +                    output[attribute_name] = attribute +                elif isinstance(attribute, list): +                    # Recursivly deal with items in lists. +                    output[attribute_name] = [ +                        self.to_representation(item) for item in attribute +                    ] +                elif isinstance(attribute, dict): +                    # Recursivly deal with items in dictionarys. +                    output[attribute_name] = { +                        str(key): self.to_representation(value) +                        for key, value in attribute.items() +                    } +                else: +                    # Force anything else to its string representation. +                    output[attribute_name] = str(attribute) + +## Serializer fields + +#### The `Field` and `ReadOnly` field classes. + +There are some minor tweaks to the field base classes. + +Previously we had these two base classes: + +* `Field` as the base class for read-only fields. A default implementation was included for serializing data. +* `WritableField` as the base class for read-write fields. + +We now use the following: + +* `Field` is the base class for all fields. It does not include any default implementation for either serializing or deserializing data. +* `ReadOnlyField` is a concrete implementation for read-only fields that simply returns the attribute value without modification. + +#### The `required`, `allow_none`, `allow_blank` and `default` arguments. + +REST framework now has more explicit and clear control over validating empty values for fields. + +Previously the meaning of the `required=False` keyword argument was underspecified. In practice its use meant that a field could either be not included in the input, or it could be included, but be `None`. + +We now have a better separation, with separate `required` and `allow_none` arguments. + +The following set of arguments are used to control validation of empty values: + +* `required=False`: The value does not need to be present in the input, and will not be passed to `.create()` or `.update()` if it is not seen. +* `default=<value>`: The value does not need to be present in the input, and a default value will be passed to `.create()` or `.update()` if it is not seen. +* `allow_none=True`: `None` is a valid input. +* `allow_blank=True`: `''` is valid input. For `CharField` and subclasses only. + +Typically you'll want to use `required=False` if the corresponding model field has a default value, and additionally set either `allow_none=True` or `allow_blank=True` if required. + +The `default` argument is there if you need it, but you'll more typically want defaults to be set on model fields, rather than serializer fields. + +#### Coercing output types. + +The previous field implementations did not forcibly coerce returned values into the correct type in many cases. For example, an `IntegerField` would return a string output if the attribute value was a string. We now more strictly coerce to the correct return type, leading to more constrained and expected behavior.  + +#### The `ListField` class. + +The `ListField` class has now been added. This field validates list input. It takes a `child` keyword argument which is used to specify the field used to validate each item in the list. For example: + +    scores = ListField(child=IntegerField(min_value=0, max_value=100)) + +You can also use a declarative style to create new subclasses of `ListField`, like this: + +    class ScoresField(ListField): +        child = IntegerField(min_value=0, max_value=100) + +We can now use the `ScoresField` class inside another serializer: + +    scores = ScoresField() + +See also the new `ListSerializer` class, which validates input in the same way, but also includes the serializer interfaces of `.is_valid()`, `.data`, `.save()` and so on. + +#### The `ChoiceField` class may now accept a flat list. + +The `ChoiceField` class may now accept a list of choices in addition to the existing style of using a list of pairs of `(name, display_value)`. The following is now valid: + +    color = ChoiceField(choices=['red', 'green', 'blue']) + +#### The `MultipleChoiceField` class. + +The `MultipleChoiceField` class has been added. This field acts like `ChoiceField`, but returns a set, which may include none, one or many of the valid choices. + +#### Changes to the custom field API. + +The `from_native(self, value)` and `to_native(self, data)` method names have been replaced with the more obviously named `to_internal_value(self, data)` and `to_representation(self, value)`. + +The `field_from_native()` and `field_to_native()` methods are removed. + +#### Explicit `queryset` required on relational fields. + +Previously relational fields that were explicitly declared on a serializer class could omit the queryset argument if (and only if) they were declared on a `ModelSerializer`. + +This code *would be valid* in `2.4.3`: + +    class AccountSerializer(serializers.ModelSerializer): +        organisations = serializers.SlugRelatedField(slug_field='name') + +        class Meta: +            model = Account + +However this code *would not be valid* in `2.4.3`: + +    # Missing `queryset` +    class AccountSerializer(serializers.Serializer): +        organisations = serializers.SlugRelatedField(slug_field='name') + +        def restore_object(self, attrs, instance=None): +            # ... + +The queryset argument is now always required for writable relational fields. +This removes some magic and makes it easier and more obvious to move between implicit `ModelSerializer` classes and explicit `Serializer` classes. + +    class AccountSerializer(serializers.ModelSerializer): +        organisations = serializers.SlugRelatedField( +            slug_field='name', +            queryset=Organisation.objects.all() +        ) + +        class Meta: +            model = Account + +The `queryset` argument is only ever required for writable fields, and is not required or valid for fields with `read_only=True`. + +#### Optional argument to `SerializerMethodField`. + +The argument to `SerializerMethodField` is now optional, and defaults to `get_<field_name>`. For example the following is valid: + +    class AccountSerializer(serializers.Serializer): +        # `method_name='get_billing_details'` by default. +        billing_details = serializers.SerializerMethodField() + +        def get_billing_details(self, account): +            return calculate_billing(account) + +In order to ensure a consistent code style an assertion error will be raised if you include a redundant method name argument that matches the default method name. For example, the following code *will raise an error*: + +    billing_details = serializers.SerializerMethodField('get_billing_details') + +#### Enforcing consistent `source` usage. + +I've see several codebases that unnecessarily include the `source` argument, setting it to the same value as the field name. This usage is redundant and confusing, making it less obvious that `source` is usually not required. + +The following usage will *now raise an error*: + +    email = serializers.EmailField(source='email') + +#### The `UniqueValidator` and `UniqueTogetherValidator` classes. + +REST framework now provides two new validators that allow you to ensure field uniqueness, while still using a completely explicit `Serializer` class instead of using `ModelSerializer`. + +The `UniqueValidator` should be applied to a serializer field, and takes a single `queryset` argument. + +    from rest_framework import serializers +    from rest_framework.validators import UniqueValidator + +    class OrganizationSerializer(serializers.Serializer): +        url = serializers.HyperlinkedIdentityField(view_name='organisation_detail') +        created = serializers.DateTimeField(read_only=True) +        name = serializers.CharField( +            max_length=100, +            validators=UniqueValidator(queryset=Organisation.objects.all()) +        ) + +The `UniqueTogetherValidator` should be applied to a serializer, and takes a `queryset` argument and a `fields` argument which should be a list or tuple of field names. + +    class RaceResultSerializer(serializers.Serializer): +        category = serializers.ChoiceField(['5k', '10k']) +        position = serializers.IntegerField() +        name = serializers.CharField(max_length=100) + +        default_validators = [UniqueTogetherValidator( +            queryset=RaceResult.objects.all(), +            fields=('category', 'position') +        )] + +## Generic views + +#### Simplification of view logic. + +The view logic for the default method handlers has been significantly simplified, due to the new serializers API. + +#### Changes to pre/post save hooks.  + +The `pre_save` and `post_save` hooks no longer exist, but are replaced with `perform_create(self, serializer)` and `perform_update(self, serializer)`. + +These methods should save the object instance by calling `serializer.save()`, adding in any additional arguments as required. They may also perform any custom pre-save or post-save behavior. + +For example: + +    def perform_create(self, serializer): +        # Include the owner attribute directly, rather than from request data. +        instance = serializer.save(owner=self.request.user) +        # Perform a custom post-save action. +        send_email(instance.to_email, instance.message) + +The `pre_delete` and `post_delete` hooks no longer exist, and are replaced with `.perform_destroy(self, instance)`, which should delete the instance and perform any custom actions. + +    def perform_destroy(self, instance): +        # Perform a custom pre-delete action. +        send_deletion_alert(user=instance.created_by, deleted=instance) +        # Delete the object instance. +        instance.delete() + +#### Removal of view attributes. + +The `.object` and `.object_list` attributes are no longer set on the view instance. Treating views as mutable object instances that store state during the processing of the view tends to be poor design, and can lead to obscure flow logic. + +I would personally recommend that developers treat view instances as immutable objects in their application code. + +#### PUT as create. + +Allowing `PUT` as create operations is problematic, as it necessarily exposes information about the existence or non-existance of objects. It's also not obvious that transparently allowing re-creating of previously deleted instances is necessarily a better default behavior than simply returning `404` responses. + +Both styles "`PUT` as 404" and "`PUT` as create" can be valid in different circumstances, but we've now opted for the 404 behavior as the default, due to it being simpler and more obvious. + +If you need to restore the previous behavior you can include the `AllowPUTAsCreateMixin` class in your view. This class can be imported from `rest_framework.mixins`. + +#### Customizing error responses. + +The generic views now raise `ValidationFailed` exception for invalid data. This exception is then dealt with by the exception handler, rather than the view returning a `400 Bad Request` response directly. + +This change means that you can now easily customize the style of error responses across your entire API, without having to modify any of the generic views. + +## The metadata API + +Behavior for dealing with `OPTIONS` requests was previously built directly into the class based views. This has now been properly separated out into a Metadata API that allows the same pluggable style as other API policies in REST framework. + +This makes it far easier to use a different style for `OPTIONS` responses throughout your API, and makes it possible to create third-party metadata policies. + +## API style + +There are some improvements in the default style we use in our API responses. + +#### Unicode JSON by default. + +Unicode JSON is now the default. The `UnicodeJSONRenderer` class no longer exists, and the `UNICODE_JSON` setting has been added. To revert this behavior use the new setting: + +    REST_FRAMEWORK = { +        'UNICODE_JSON': False +    } + +#### Compact JSON by default. + +We now output compact JSON in responses by default. For example, we return: + +    {"email":"amy@example.com","is_admin":true} + +Instead of the following: + +    {"email": "amy@example.com", "is_admin": true} + +The `COMPACT_JSON` setting has been added, and can be used to revert this behavior if needed: + +    REST_FRAMEWORK = { +        'COMPACT_JSON': False +    } + +#### File fields as URLs + +The `FileField` and `ImageField` classes are now represented as URLs by default. You should ensure you set Django's [standard `MEDIA_URL` setting](https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-MEDIA_URL) appropriately, and ensure your application [serves the uploaded files](https://docs.djangoproject.com/en/dev/howto/static-files/#serving-uploaded-files-in-development). + +You can revert this behavior, and display filenames in the representation by using the `UPLOADED_FILES_USE_URL` settings key: + +    REST_FRAMEWORK = { +        'UPLOADED_FILES_USE_URL': False +    } + +You can also modify serializer fields individually, using the `use_url` argument: + +    uploaded_file = serializers.FileField(user_url=False) + +Also note that you should pass the `request` object to the serializer as context when instantiating it, so that a fully qualified URL can be returned. Returned URLs will then be of the form `https://example.com/url_path/filename.txt`. For example: + +    context = {'request': request} +    serializer = ExampleSerializer(instance, context=context) +    return Response(serializer.data) + +If the request is omitted from the context, the returned URLs will be of the form `/url_path/filename.txt`. + +#### Throttle headers using `Retry-After`. + +The custom `X-Throttle-Wait-Second` header has now been dropped in favor of the standard `Retry-After` header. You can revert this behavior if needed by writing a custom exception handler for your application. + +#### Date and time objects as ISO-8859-1 strings in serializer data. + +Date and Time objects are now coerced to strings by default in the serializer output. Previously they were returned as `Date`, `Time` and `DateTime` objects, and later coerced to strings by the renderer. + +You can modify this behavior globally by settings the existing `DATE_FORMAT`, `DATETIME_FORMAT` and `TIME_FORMAT` settings keys. Setting these values to `None` instead of their default value of `'iso-8859-1'` will result in native objects being returned in serializer data. + +    REST_FRAMEWORK = { +        # Return native `Date` and `Time` objects in `serializer.data` +        'DATETIME_FORMAT': None +        'DATE_FORMAT': None +        'TIME_FORMAT': None +    } + +You can also modify serializer fields individually, using the `date_format`, `time_format` and `datetime_format` arguments: + +    # Return `DateTime` instances in `serializer.data`, not strings. +    created = serializers.DateTimeField(format=None) + +#### Decimals as strings in serializer data. + +Decimals are now coerced to strings by default in the serializer output. Previously they were returned as `Decimal` objects, and later coerced to strings by the renderer. + +You can modify this behavior globally by using the `COERCE_DECIMAL_TO_STRING` settings key. + +    REST_FRAMEWORK = { +        'COERCE_DECIMAL_TO_STRING': False +    } + +Or modify it on an individual serializer field, using the `corece_to_string` keyword argument. + +    # Return `Decimal` instances in `serializer.data`, not strings. +    amount = serializers.DecimalField( +        max_digits=10, +        decimal_places=2, +        coerce_to_string=False +    ) + +The default JSON renderer will return float objects for uncoerced `Decimal` instances. This allows you to easily switch between string or float representations for decimals depending on your API design needs. + +## What's coming next. + +3.0 is an incremental release, and there are several upcoming features that will build on the baseline improvements that it makes. + +The 3.1 release is planned to address improvements in the following components: + +* Request parsing, mediatypes & the implementation of the browsable API. +* Introduction of a new pagination API. +* Better support for API versioning. + +The 3.2 release is planned to introduce an alternative admin-style interface to the browsable API. + +You can follow development on the GitHub site, where we use [milestones to indicate planning timescales](https://github.com/tomchristie/django-rest-framework/milestones). diff --git a/docs/topics/contributing.md b/docs/topics/contributing.md index 4fafb1b1..96d9a98c 100644 --- a/docs/topics/contributing.md +++ b/docs/topics/contributing.md @@ -109,7 +109,7 @@ You can also use the excellent [tox][tox] testing tool to run the tests against  It's a good idea to make pull requests early on.  A pull request represents the start of a discussion, and doesn't necessarily need to be the final, finished submission. -It's also always best to make a new branch before starting work on a pull request.  This means that you'll be able to later switch back to working on another seperate issue without interfering with an ongoing pull requests. +It's also always best to make a new branch before starting work on a pull request.  This means that you'll be able to later switch back to working on another separate issue without interfering with an ongoing pull requests.  It's also useful to remember that if you have an outstanding pull request then pushing new commits to your GitHub repo will also automatically update the pull requests. diff --git a/docs/topics/release-notes.md b/docs/topics/release-notes.md index 16589f3b..4fa3d627 100644 --- a/docs/topics/release-notes.md +++ b/docs/topics/release-notes.md @@ -149,7 +149,7 @@ You can determine your currently installed version using `pip freeze`:  * Added `write_only_fields` option to `ModelSerializer` classes.  * JSON renderer now deals with objects that implement a dict-like interface.  * Fix compatiblity with newer versions of `django-oauth-plus`. -* Bugfix: Refine behavior that calls model manager `all()` across nested serializer relationships, preventing erronous behavior with some non-ORM objects, and preventing unneccessary queryset re-evaluations. +* Bugfix: Refine behavior that calls model manager `all()` across nested serializer relationships, preventing erronous behavior with some non-ORM objects, and preventing unnecessary queryset re-evaluations.  * Bugfix: Allow defaults on BooleanFields to be properly honored when values are not supplied.  * Bugfix: Prevent double-escaping of non-latin1 URL query params when appending `format=json` params. diff --git a/docs/topics/writable-nested-serializers.md b/docs/topics/writable-nested-serializers.md index 66ea7815..abc6a82f 100644 --- a/docs/topics/writable-nested-serializers.md +++ b/docs/topics/writable-nested-serializers.md @@ -6,7 +6,7 @@  Although flat data structures serve to properly delineate between the individual entities in your service, there are cases where it may be more appropriate or convenient to use nested data structures. -Nested data structures are easy enough to work with if they're read-only - simply nest your serializer classes and you're good to go.  However, there are a few more subtleties to using writable nested serializers, due to the dependancies between the various model instances, and the need to save or delete multiple instances in a single action. +Nested data structures are easy enough to work with if they're read-only - simply nest your serializer classes and you're good to go.  However, there are a few more subtleties to using writable nested serializers, due to the dependencies between the various model instances, and the need to save or delete multiple instances in a single action.  ## One-to-many data structures  diff --git a/docs/tutorial/1-serialization.md b/docs/tutorial/1-serialization.md index b0565d91..db5b9ea7 100644 --- a/docs/tutorial/1-serialization.md +++ b/docs/tutorial/1-serialization.md @@ -41,20 +41,7 @@ Once that's done we can create an app that we'll use to create a simple Web API.      python manage.py startapp snippets -The simplest way to get up and running will probably be to use an `sqlite3` database for the tutorial.  Edit the `tutorial/settings.py` file, and set the default database `"ENGINE"` to `"sqlite3"`, and `"NAME"` to `"tmp.db"`. - -    DATABASES = { -        'default': { -            'ENGINE': 'django.db.backends.sqlite3', -            'NAME': 'tmp.db', -            'USER': '', -            'PASSWORD': '', -            'HOST': '', -            'PORT': '', -        } -    } - -We'll also need to add our new `snippets` app and the `rest_framework` app to `INSTALLED_APPS`. +We'll need to add our new `snippets` app and the `rest_framework` app to `INSTALLED_APPS`. Let's edit the `tutorial/settings.py` file:      INSTALLED_APPS = (          ... @@ -72,7 +59,7 @@ Okay, we're ready to roll.  ## Creating a model to work with -For the purposes of this tutorial we're going to start by creating a simple `Snippet` model that is used to store code snippets.  Go ahead and edit the `snippets` app's `models.py` file.  Note: Good programming practices include comments.  Although you will find them in our repository version of this tutorial code, we have omitted them here to focus on the code itself. +For the purposes of this tutorial we're going to start by creating a simple `Snippet` model that is used to store code snippets.  Go ahead and edit the `snippets/models.py` file.  Note: Good programming practices include comments.  Although you will find them in our repository version of this tutorial code, we have omitted them here to focus on the code itself.      from django.db import models      from pygments.lexers import get_all_lexers @@ -98,9 +85,10 @@ For the purposes of this tutorial we're going to start by creating a simple `Sni          class Meta:              ordering = ('created',) -Don't forget to sync the database for the first time. +We'll also need to create an initial migration for our snippet model, and sync the database for the first time. -    python manage.py syncdb +    python manage.py makemigrations snippets +    python manage.py migrate  ## Creating a Serializer class @@ -112,40 +100,39 @@ The first thing we need to get started on our Web API is to provide a way of ser      class SnippetSerializer(serializers.Serializer): -        pk = serializers.Field()  # Note: `Field` is an untyped read-only field. +        pk = serializers.IntegerField(read_only=True)          title = serializers.CharField(required=False,                                        max_length=100) -        code = serializers.CharField(widget=widgets.Textarea, -                                     max_length=100000) +        code = serializers.CharField(style={'type': 'textarea'})          linenos = serializers.BooleanField(required=False)          language = serializers.ChoiceField(choices=LANGUAGE_CHOICES,                                             default='python')          style = serializers.ChoiceField(choices=STYLE_CHOICES,                                          default='friendly') -        def restore_object(self, attrs, instance=None): +        def create(self, validated_attrs):              """ -            Create or update a new snippet instance, given a dictionary -            of deserialized field values. +            Create and return a new `Snippet` instance, given the validated data. +            """ +            return Snippet.objects.create(**validated_attrs) -            Note that if we don't define this method, then deserializing -            data will simply return a dictionary of items. +        def update(self, instance, validated_attrs): +            """ +            Update and return an existing `Snippet` instance, given the validated data.              """ -            if instance: -                # Update existing instance -                instance.title = attrs.get('title', instance.title) -                instance.code = attrs.get('code', instance.code) -                instance.linenos = attrs.get('linenos', instance.linenos) -                instance.language = attrs.get('language', instance.language) -                instance.style = attrs.get('style', instance.style) -                return instance +            instance.title = validated_attrs.get('title', instance.title) +            instance.code = validated_attrs.get('code', instance.code) +            instance.linenos = validated_attrs.get('linenos', instance.linenos) +            instance.language = validated_attrs.get('language', instance.language) +            instance.style = validated_attrs.get('style', instance.style) +            instance.save() +            return instance -            # Create new instance -            return Snippet(**attrs) +The first part of the serializer class defines the fields that get serialized/deserialized.  The `create()` and `update()` methods define how fully fledged instances are created or modified when calling `serializer.save()` -The first part of the serializer class defines the fields that get serialized/deserialized.  The `restore_object` method defines how fully fledged instances get created when deserializing data. +A serializer class is very similar to a Django `Form` class, and includes similar validation flags on the various fields, such as `required`, `max_length` and `default`. -Notice that we can also use various attributes that would typically be used on form fields, such as `widget=widgets.Textarea`.  These can be used to control how the serializer should render when displayed as an HTML form.  This is particularly useful for controlling how the browsable API should be displayed, as we'll see later in the tutorial. +The field flags can also control how the serializer should be displayed in certain circumstances, such as when rendering to HTML. The `style={'type': 'textarea'}` flag above is equivelent to using `widget=widgets.Textarea` on a Django `Form` class. This is particularly useful for controlling how the browsable API should be displayed, as we'll see later in the tutorial.  We can actually also save ourselves some time by using the `ModelSerializer` class, as we'll see later, but for now we'll keep our serializer definition explicit.   @@ -219,6 +206,24 @@ Open the file `snippets/serializers.py` again, and edit the `SnippetSerializer`              model = Snippet              fields = ('id', 'title', 'code', 'linenos', 'language', 'style') +Once nice property that serializers have is that you can inspect all the fields an serializer instance, by printing it's representation. Open the Django shell with `python manange.py shell`, then try the following: + +    >>> from snippets.serializers import SnippetSerializer +    >>> serializer = SnippetSerializer() +    >>> print repr(serializer)  # In python 3 use `print(repr(serializer))` +    SnippetSerializer(): +        id = IntegerField(label='ID', read_only=True) +        title = CharField(allow_blank=True, max_length=100, required=False) +        code = CharField(style={'type': 'textarea'}) +        linenos = BooleanField(required=False) +        language = ChoiceField(choices=[('Clipper', 'FoxPro'), ('Cucumber', 'Gherkin'), ('RobotFramework', 'RobotFramework'), ('abap', 'ABAP'), ('ada', 'Ada')... +        style = ChoiceField(choices=[('autumn', 'autumn'), ('borland', 'borland'), ('bw', 'bw'), ('colorful', 'colorful')... + +It's important to remember that `ModelSerializer` classes don't do anything particularly magically, they are simply a shortcut to creating a serializer class with: + +* An automatically determined set of fields. +* Simple default implementations for the `create()` and `update()` methods. +  ## Writing regular Django views using our Serializer  Let's see how we can write some API views using our new Serializer class. diff --git a/docs/tutorial/4-authentication-and-permissions.md b/docs/tutorial/4-authentication-and-permissions.md index 9120e254..adab1b55 100644 --- a/docs/tutorial/4-authentication-and-permissions.md +++ b/docs/tutorial/4-authentication-and-permissions.md @@ -92,24 +92,26 @@ Finally we need to add those views into the API, by referencing them from the UR  Right now, if we created a code snippet, there'd be no way of associating the user that created the snippet, with the snippet instance.  The user isn't sent as part of the serialized representation, but is instead a property of the incoming request. -The way we deal with that is by overriding a `.pre_save()` method on our snippet views, that allows us to handle any information that is implicit in the incoming request or requested URL. +The way we deal with that is by overriding a `.perform_create()` method on our snippet views, that allows us to modify how the instance save is managed, and handle any information that is implicit in the incoming request or requested URL. -On **both** the `SnippetList` and `SnippetDetail` view classes, add the following method: +On the `SnippetList` view class, add the following method: -    def pre_save(self, obj): -        obj.owner = self.request.user +    def perform_create(self, serializer): +        serializer.save(owner=self.request.user) + +The `create()` method of our serializer will now be passed an additional `'owner'` field, along with the validated data from the request.  ## Updating our serializer  Now that snippets are associated with the user that created them, let's update our `SnippetSerializer` to reflect that.  Add the following field to the serializer definition in `serializers.py`: -    owner = serializers.Field(source='owner.username') +    owner = serializers.ReadOnlyField(source='owner.username')  **Note**: Make sure you also add `'owner',` to the list of fields in the inner `Meta` class.  This field is doing something quite interesting.  The `source` argument controls which attribute is used to populate a field, and can point at any attribute on the serialized instance.  It can also take the dotted notation shown above, in which case it will traverse the given attributes, in a similar way as it is used with Django's template language. -The field we've added is the untyped `Field` class, in contrast to the other typed fields, such as `CharField`, `BooleanField` etc...  The untyped `Field` is always read-only, and will be used for serialized representations, but will not be used for updating model instances when they are deserialized. +The field we've added is the untyped `ReadOnlyField` class, in contrast to the other typed fields, such as `CharField`, `BooleanField` etc...  The untyped `ReadOnlyField` is always read-only, and will be used for serialized representations, but will not be used for updating model instances when they are deserialized. We could have also used `CharField(read_only=True)` here.  ## Adding required permissions to views diff --git a/docs/tutorial/quickstart.md b/docs/tutorial/quickstart.md index 813e9872..c2dc4bea 100644 --- a/docs/tutorial/quickstart.md +++ b/docs/tutorial/quickstart.md @@ -26,11 +26,13 @@ Create a new Django project named `tutorial`, then start a new app called `quick  Now sync your database for the first time: -    python manage.py syncdb +    python manage.py migrate -Make sure to create an initial user named `admin` with a password of `password`. We'll authenticate as that user later in our example. +We'll also create an initial user named `admin` with a password of `password`. We'll authenticate as that user later in our example. -Once you've set up a database and got everything synced and ready to go, open up the app's directory and we'll get coding... +    python manage.py createsuperuser + +Once you've set up a database and initial user created and ready to go, open up the app's directory and we'll get coding...  ## Serializers | 
