From dd14c6c88ba210bac8349041b8db486576539249 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 3 Nov 2014 11:21:29 +0000 Subject: [PATCH] Latest docs release --- api-guide/authentication.html | 15 +- api-guide/format-suffixes.html | 11 +- api-guide/metadata.html | 324 +++++++ api-guide/validators.html | 388 ++++++++ index.html | 6 +- topics/2.3-announcement.html | 6 +- topics/2.4-announcement.html | 4 +- topics/3.0-announcement.html | 886 ++++++++++++++++++ topics/contributing.html | 3 +- topics/release-notes.html | 9 + topics/third-party-resources.html | 1 + tutorial/1-serialization.html | 13 +- tutorial/2-requests-and-responses.html | 9 +- tutorial/3-class-based-views.html | 4 +- .../4-authentication-and-permissions.html | 4 +- .../5-relationships-and-hyperlinked-apis.html | 10 +- tutorial/6-viewsets-and-routers.html | 12 +- 17 files changed, 1661 insertions(+), 44 deletions(-) create mode 100644 api-guide/metadata.html create mode 100644 api-guide/validators.html create mode 100644 topics/3.0-announcement.html diff --git a/api-guide/authentication.html b/api-guide/authentication.html index 3118ff341..420a1874b 100644 --- a/api-guide/authentication.html +++ b/api-guide/authentication.html @@ -206,6 +206,7 @@ a.fusion-poweredby {
  • JSON Web Token Authentication
  • Hawk HTTP Authentication
  • HTTP Signature Authentication
  • +
  • Djoser
  • @@ -337,12 +338,13 @@ print token.key

    Generating Tokens

    If you want every user to have an automatically generated Token, you can simply catch the User's post_save signal.

    -
    from django.contrib.auth import get_user_model
    +
    from django.conf import settings
    +from django.contrib.auth import get_user_model
     from django.db.models.signals import post_save
     from django.dispatch import receiver
     from rest_framework.authtoken.models import Token
     
    -@receiver(post_save, sender=get_user_model())
    +@receiver(post_save, sender=settings.AUTH_USER_MODEL)
     def create_auth_token(sender, instance=None, created=False, **kwargs):
         if created:
             Token.objects.create(user=instance)
    @@ -356,9 +358,10 @@ for user in User.objects.all():
         Token.objects.get_or_create(user=user)
     

    When using TokenAuthentication, you may want to provide a mechanism for clients to obtain a token given the username and password. REST framework provides a built-in view to provide this behavior. To use it, add the obtain_auth_token view to your URLconf:

    -
    urlpatterns += patterns('',
    -    url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token')
    -)
    +
    from rest_framework.authtoken import views
    +urlpatterns += [
    +    url(r'^api-token-auth/', views.obtain_auth_token)
    +]
     

    Note that the URL part of the pattern can be whatever you want to use.

    The obtain_auth_token view will return a JSON response when valid username and password fields are POSTed to the view using form data or JSON:

    @@ -508,6 +511,8 @@ class ExampleAuthentication(authentication.BaseAuthentication):

    The HawkREST library builds on the Mohawk library to let you work with Hawk signed requests and responses in your API. Hawk lets two parties securely communicate with each other using messages signed by a shared key. It is based on HTTP MAC access authentication (which was based on parts of OAuth 1.0).

    HTTP Signature Authentication

    HTTP Signature (currently a IETF draft) provides a way to achieve origin authentication and message integrity for HTTP messages. Similar to Amazon's HTTP Signature scheme, used by many of its services, it permits stateless, per-request authentication. Elvio Toccalino maintains the djangorestframework-httpsignature package which provides an easy to use HTTP Signature Authentication mechanism.

    +

    Djoser

    +

    Djoser library provides a set of views to handle basic actions such as registration, login, logout, password reset and account activation. The package works with a custom user model and it uses token based authentication. This is a ready to use REST implementation of Django authentication system.

    diff --git a/api-guide/format-suffixes.html b/api-guide/format-suffixes.html index 47ed1a5c0..0531f75c7 100644 --- a/api-guide/format-suffixes.html +++ b/api-guide/format-suffixes.html @@ -219,12 +219,13 @@ used all the time.

    Example:

    from rest_framework.urlpatterns import format_suffix_patterns
    +from blog import views
     
    -urlpatterns = patterns('blog.views',
    -    url(r'^/$', 'api_root'),
    -    url(r'^comments/$', 'comment_list'),
    -    url(r'^comments/(?P<pk>[0-9]+)/$', 'comment_detail')
    -)
    +urlpatterns = [
    +    url(r'^/$', views.apt_root),
    +    url(r'^comments/$', views.comment_list),
    +    url(r'^comments/(?P<pk>[0-9]+)/$', views.comment_detail)
    +]
     
     urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html'])
     
    diff --git a/api-guide/metadata.html b/api-guide/metadata.html new file mode 100644 index 000000000..1ca656b7f --- /dev/null +++ b/api-guide/metadata.html @@ -0,0 +1,324 @@ + + + + + Metadata - Django REST framework + + + + + + + + + + + + + + + + + + + + +
    + + + +
    +
    + + + + +
    + + + +
    +

    metadata.py

    +

    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.

    +
    +

    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.

    +
    +

    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, 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()
    +        }
    +
    +
    +
    +
    +
    + +
    +
    + + + + + + + + + + + diff --git a/api-guide/validators.html b/api-guide/validators.html new file mode 100644 index 000000000..de19736e1 --- /dev/null +++ b/api-guide/validators.html @@ -0,0 +1,388 @@ + + + + + Validators - Django REST framework + + + + + + + + + + + + + + + + + + + + +
    + + + +
    +
    + + + + +
    + + + +
    +

    validators.py

    +

    Validators

    +
    +

    Validators can be useful for re-using validation logic between different types of fields.

    +

    Django documentation

    +
    +

    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.

    +

    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
    +
    +
    +
    +
    +
    + +
    +
    + + + + + + + + + + + diff --git a/index.html b/index.html index 5cc86df97..800bece84 100644 --- a/index.html +++ b/index.html @@ -253,7 +253,7 @@ a.fusion-poweredby {

    Requirements

    REST framework requires the following:

      -
    • Python (2.6.5+, 2.7, 3.2, 3.3)
    • +
    • Python (2.6.5+, 2.7, 3.2, 3.3, 3.4)
    • Django (1.4.2+, 1.5, 1.6, 1.7)

    The following packages are optional:

    @@ -283,10 +283,10 @@ pip install django-filter # Filtering support )

    If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root urls.py file.

    -
    urlpatterns = patterns('',
    +
    urlpatterns = [
         ...
         url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
    -)
    +]
     

    Note that the URL path can be whatever you want, but you must include 'rest_framework.urls' with the 'rest_framework' namespace.

    Example

    diff --git a/topics/2.3-announcement.html b/topics/2.3-announcement.html index 5fb6e05f0..27da50d8a 100644 --- a/topics/2.3-announcement.html +++ b/topics/2.3-announcement.html @@ -223,7 +223,7 @@ a.fusion-poweredby {
    """
     A REST framework API for viewing and editing users and groups.
     """
    -from django.conf.urls.defaults import url, patterns, include
    +from django.conf.urls.defaults import url, include
     from django.contrib.auth.models import User, Group
     from rest_framework import viewsets, routers
     
    @@ -244,10 +244,10 @@ router.register(r'groups', GroupViewSet)
     
     # Wire up our API using automatic URL routing.
     # Additionally, we include login URLs for the browseable API.
    -urlpatterns = patterns('',
    +urlpatterns = [
         url(r'^', include(router.urls)),
         url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
    -)
    +]
     

    The best place to get started with ViewSets and Routers is to take a look at the newest section in the tutorial, which demonstrates their usage.

    Simpler views

    diff --git a/topics/2.4-announcement.html b/topics/2.4-announcement.html index c0fa26d0f..868f718cf 100644 --- a/topics/2.4-announcement.html +++ b/topics/2.4-announcement.html @@ -295,8 +295,8 @@ The lowest supported version of Django is now 1.4.2.

    Other features

    There are also a number of other features and bugfixes as listed in the release notes. In particular these include:

    -

    Customizable view name and description functions for use with the browsable API, by using the VIEW_NAME_FUNCTION and VIEW_DESCRIPTION_FUNCTION settings.

    -

    Smarter client IP identification for throttling, with the addition of the NUM_PROXIES setting.

    +

    Customizable view name and description functions for use with the browsable API, by using the VIEW_NAME_FUNCTION and VIEW_DESCRIPTION_FUNCTION settings.

    +

    Smarter client IP identification for throttling, with the addition of the NUM_PROXIES setting.

    Added the standardized Retry-After header to throttled responses, as per RFC 6585. This should now be used in preference to the custom X-Throttle-Wait-Seconds header which will be fully deprecated in 3.0.

    Deprecations

    All API changes in 2.3 that previously raised PendingDeprecationWarning will now raise a DeprecationWarning, which is loud by default.

    diff --git a/topics/3.0-announcement.html b/topics/3.0-announcement.html new file mode 100644 index 000000000..3ced0ce6c --- /dev/null +++ b/topics/3.0-announcement.html @@ -0,0 +1,886 @@ + + + + + Pre-release notes: - Django REST framework + + + + + + + + + + + + + + + + + + + + +
    + + + +
    +
    + + + + +
    + + + +
    +

    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 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. +
    3. Calling serializer.save() would then save the object instance to the database.
    4. +
    +

    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. +
    3. Calling serializer.save() then saves and returns the new object instance.
    4. +
    +

    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 appropriately, and ensure your application serves the uploaded files.

    +

    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.

    +
    +
    +
    +
    + +
    +
    + + + + + + + + + + + diff --git a/topics/contributing.html b/topics/contributing.html index a86baac81..85d9736d7 100644 --- a/topics/contributing.html +++ b/topics/contributing.html @@ -356,7 +356,8 @@ More text...

    If you have some functionality that you would like to implement as a third party package it's worth contacting the discussion group as others may be willing to get involved. We strongly encourage third party package development and will always try to prioritize time spent helping their development, documentation and packaging.

    We recommend the django-reusable-app template as a good resource for getting up and running with implementing a third party Django package.

    Linking to your package

    -

    Once your package is decently documented and available on PyPI open a pull request or issue, and we'll add a link to it from the main REST framework documentation.

    +

    Once your package is decently documented and available on PyPI open a pull request or issue, and we'll add a link to it from the main REST framework documentation. You can add your package under Third party packages of the API Guide section that best applies, like Authentication or Permissions. You can also link your package under the Third Party Resources section.

    +

    We also suggest adding it to the REST Framework grid on Django Packages.

    diff --git a/topics/release-notes.html b/topics/release-notes.html index 4f8ebe661..d3c6a059b 100644 --- a/topics/release-notes.html +++ b/topics/release-notes.html @@ -242,6 +242,15 @@ a.fusion-poweredby {

    2.4.x series

    +

    2.4.4

    +

    Date: 3rd November 2014.

    +

    2.4.3

    Date: 19th September 2014.

    Permissions

    After adding all those names into our URLconf, our final snippets/urls.py file should look something like this:

    # API endpoints
    -urlpatterns = format_suffix_patterns(patterns('snippets.views',
    -    url(r'^$', 'api_root'),
    +urlpatterns = format_suffix_patterns([
    +    url(r'^$', views.api_root),
         url(r'^snippets/$',
             views.SnippetList.as_view(),
             name='snippet-list'),
    @@ -310,13 +310,13 @@ urlpatterns = format_suffix_patterns(patterns('snippets.views',
         url(r'^users/(?P<pk>[0-9]+)/$',
             views.UserDetail.as_view(),
             name='user-detail')
    -))
    +])
     
     # Login and logout views for the browsable API
    -urlpatterns += patterns('',
    +urlpatterns += [
         url(r'^api-auth/', include('rest_framework.urls',
                                    namespace='rest_framework')),
    -)
    +]
     

    Adding pagination

    The list views for users and code snippets could end up returning quite a lot of instances, so really we'd like to make sure we paginate the results, and allow the API client to step through each of the individual pages.

    diff --git a/tutorial/6-viewsets-and-routers.html b/tutorial/6-viewsets-and-routers.html index f8d2f5eb5..da4c63834 100644 --- a/tutorial/6-viewsets-and-routers.html +++ b/tutorial/6-viewsets-and-routers.html @@ -275,19 +275,19 @@ user_detail = UserViewSet.as_view({

    Notice how we're creating multiple views from each ViewSet class, by binding the http methods to the required action for each view.

    Now that we've bound our resources into concrete views, we can register the views with the URL conf as usual.

    -
    urlpatterns = format_suffix_patterns(patterns('snippets.views',
    -    url(r'^$', 'api_root'),
    +
    urlpatterns = format_suffix_patterns([
    +    url(r'^$', api_root),
         url(r'^snippets/$', snippet_list, name='snippet-list'),
         url(r'^snippets/(?P<pk>[0-9]+)/$', snippet_detail, name='snippet-detail'),
         url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'),
         url(r'^users/$', user_list, name='user-list'),
         url(r'^users/(?P<pk>[0-9]+)/$', user_detail, name='user-detail')
    -))
    +])
     

    Using Routers

    Because we're using ViewSet classes rather than View classes, we actually don't need to design the URL conf ourselves. The conventions for wiring up resources into views and urls can be handled automatically, using a Router class. All we need to do is register the appropriate view sets with a router, and let it do the rest.

    Here's our re-wired urls.py file.

    -
    from django.conf.urls import patterns, url, include
    +
    from django.conf.urls import url, include
     from snippets import views
     from rest_framework.routers import DefaultRouter
     
    @@ -298,10 +298,10 @@ router.register(r'users', views.UserViewSet)
     
     # The API URLs are now determined automatically by the router.
     # Additionally, we include the login URLs for the browseable API.
    -urlpatterns = patterns('',
    +urlpatterns = [
         url(r'^', include(router.urls)),
         url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
    -)
    +]
     

    Registering the viewsets with the router is similar to providing a urlpattern. We include two arguments - the URL prefix for the views, and the viewset itself.

    The DefaultRouter class we're using also automatically creates the API root view for us, so we can now delete the api_root method from our views module.