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.
+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):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.
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()
.
ChoiceField
does not currently display nested choices, as was the case in 2.4. This will be address as part of 3.1.pip freeze | grep djangorestframework
Date: 1st December 2014
+For full details see the 3.0 release announcement.
+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 highlightWhen 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()),