diff --git a/api-guide/serializers/index.html b/api-guide/serializers/index.html index 08a68bb08..e147cbc33 100644 --- a/api-guide/serializers/index.html +++ b/api-guide/serializers/index.html @@ -943,6 +943,14 @@ AccountSerializer(): read_only_fields = ('account_name',)

Model fields which have editable=False set, and AutoField fields will be set to read-only by default, and do not need to be added to the read_only_fields option.

+
+

Note: There is a special-case where a read-only field is part of a unique_together constraint at the model level. In this case the field is required by the serializer class in order to validate the constraint, but should also not be editable by the user.

+

The right way to deal with this is to specify the field explicitly on the serializer, providing both the read_only=True and default=… keyword arguments.

+

One example of this is a read-only relation to the currently authenticated User which is unique_together with another identifier. In this case you would declare the user field like so:

+
user = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CurrentUserDefault())
+
+

Please review the Validators Documentation for details on the UniqueTogetherValidator and CurrentUserDefault classes.

+

Specifying additional keyword arguments for fields.

There is also a shortcut allowing you to specify arbitrary additional keyword arguments on fields, using the extra_kwargs option. Similarly to read_only_fields this means you do not need to explicitly declare the field on the serializer.

This option is a dictionary, mapping field names to a dictionary of keyword arguments. For example:

@@ -1057,7 +1065,7 @@ class BookSerializer(serializers.Serializer):
  • How do you determine which instance should be updated for each item in the list of data?
  • How should insertions be handled? Are they invalid, or do they create new objects?
  • How should removals be handled? Do they imply object deletion, or removing a relationship? Should they be silently ignored, or are they invalid?
  • -
  • How should ordering be handled? Does changing the position of two items imply any state change or is it ignored?
  • +
  • How should ordering be handled? Does changing the position of two items imply any state change or is it ignored?
  • Here's an example of how you might choose to implement multiple updates:

    class BookListSerializer(serializers.ListSerializer):
    diff --git a/api-guide/viewsets/index.html b/api-guide/viewsets/index.html
    index c293a4c2f..85dbe2990 100644
    --- a/api-guide/viewsets/index.html
    +++ b/api-guide/viewsets/index.html
    @@ -579,6 +579,7 @@ class UserViewSet(viewsets.ModelViewSet):
         def get_queryset(self):
             return self.request.user.accounts.all()
     
    +

    Note however that upon removal of the queryset property from your ViewSet, any associated router will be unable to derive the base_name of your Model automatically, and so you you will have to specify the base_name kwarg as part of your router registration.

    Also note that although this class provides the complete set of create/list/retrieve/update/destroy actions by default, you can restrict the available operations by using the standard permission classes.

    ReadOnlyModelViewSet

    The ReadOnlyModelViewSet class also inherits from GenericAPIView. As with ModelViewSet it also includes implementations for various actions, but unlike ModelViewSet only provides the 'read-only' actions, .list() and .retrieve().

    diff --git a/topics/3.0-announcement/index.html b/topics/3.0-announcement/index.html index 0071c4361..c2f612d29 100644 --- a/topics/3.0-announcement/index.html +++ b/topics/3.0-announcement/index.html @@ -1122,6 +1122,7 @@ amount = serializers.DecimalField(

    What's coming next

    diff --git a/topics/release-notes/index.html b/topics/release-notes/index.html index e3f9c67e5..64188debb 100644 --- a/topics/release-notes/index.html +++ b/topics/release-notes/index.html @@ -361,6 +361,10 @@ Upgrading +
  • + 3.0.x series +
  • +
  • 2.4.x series
  • @@ -426,6 +430,11 @@
    pip freeze | grep djangorestframework
     

    +

    3.0.x series

    +

    3.0.0

    +

    Date: 1st December 2014

    +

    For full details see the 3.0 release announcement.

    +

    2.4.x series

    2.4.4

    Date: 3rd November 2014.

    diff --git a/tutorial/4-authentication-and-permissions/index.html b/tutorial/4-authentication-and-permissions/index.html index df92f540a..2efc72453 100644 --- a/tutorial/4-authentication-and-permissions/index.html +++ b/tutorial/4-authentication-and-permissions/index.html @@ -435,7 +435,7 @@ from pygments import highlight

    When that's all done we'll need to update our database tables. Normally we'd create a database migration in order to do that, but for the purposes of this tutorial, let's just delete the database and start again.

    -
    rm tmp.db
    +
    rm -f tmp.db db.sqlite3
     rm -r snippets/migrations
     python manage.py makemigrations snippets
     python manage.py migrate
    @@ -448,7 +448,7 @@ python manage.py migrate
     
    from django.contrib.auth.models import User
     
     class UserSerializer(serializers.ModelSerializer):
    -    snippets = serializers.PrimaryKeyRelatedField(many=True)
    +    snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all())
     
         class Meta:
             model = User
    diff --git a/tutorial/5-relationships-and-hyperlinked-apis/index.html b/tutorial/5-relationships-and-hyperlinked-apis/index.html
    index 80e840044..364de40df 100644
    --- a/tutorial/5-relationships-and-hyperlinked-apis/index.html
    +++ b/tutorial/5-relationships-and-hyperlinked-apis/index.html
    @@ -421,7 +421,7 @@ class SnippetHighlight(generics.GenericAPIView):
     

    As usual we need to add the new views that we've created in to our URLconf. We'll add a url pattern for our new API root in snippets/urls.py:

    -
    url(r'^$', 'api_root'),
    +
    url(r'^$', views.api_root),
     

    And then add a url pattern for the snippet highlights:

    url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', views.SnippetHighlight.as_view()),