From ffa25009fd06b053e99c14960643f4c5f2c5092a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 10:11:44 +0100 Subject: 2.4 release --- tutorial/1-serialization.html | 1 + tutorial/2-requests-and-responses.html | 1 + tutorial/3-class-based-views.html | 1 + tutorial/4-authentication-and-permissions.html | 1 + tutorial/5-relationships-and-hyperlinked-apis.html | 1 + tutorial/6-viewsets-and-routers.html | 1 + tutorial/quickstart.html | 41 +++++++++------------- 7 files changed, 22 insertions(+), 25 deletions(-) (limited to 'tutorial') diff --git a/tutorial/1-serialization.html b/tutorial/1-serialization.html index 53401303..f5eff13c 100644 --- a/tutorial/1-serialization.html +++ b/tutorial/1-serialization.html @@ -121,6 +121,7 @@ a.fusion-poweredby {
Next you'll need to get a database set up and synced. If you just want to use SQLite for now, then you'll want to edit your tutorial/settings.py module to include something like this:
DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.sqlite3',
- 'NAME': 'database.sql',
- 'USER': '',
- 'PASSWORD': '',
- 'HOST': '',
- 'PORT': ''
- }
-}
+# Set up a new project with a single application
+django-admin.py startproject tutorial .
+cd tutorial
+django-admin.py startapp quickstart
+cd ..
-The run syncdb like so:
Now sync your database for the first time:
python manage.py syncdb
+Make sure to 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...
First up we're going to define some serializers in quickstart/serializers.py that we'll use for our data representations.
First up we're going to define some serializers. Let's create a new module named tutorial/quickstart/serializers.py that we'll use for our data representations.
from django.contrib.auth.models import User, Group
from rest_framework import serializers
@@ -258,10 +248,10 @@ class GroupSerializer(serializers.HyperlinkedModelSerializer):
Notice that we're using hyperlinked relations in this case, with HyperlinkedModelSerializer. You can also use primary key and various other relationships, but hyperlinking is good RESTful design.
Right, we'd better write some views then. Open quickstart/views.py and get typing.
Right, we'd better write some views then. Open tutorial/quickstart/views.py and get typing.
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
-from quickstart.serializers import UserSerializer, GroupSerializer
+from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
class UserViewSet(viewsets.ModelViewSet):
@@ -285,9 +275,9 @@ class GroupViewSet(viewsets.ModelViewSet):
For trivial cases you can simply set a model attribute on the ViewSet class and the serializer and queryset will be automatically generated for you. Setting the queryset and/or serializer_class attributes gives you more explicit control of the API behaviour, and is the recommended style for most applications.
URLs
Okay, now let's wire up the API URLs. On to tutorial/urls.py...
-from django.conf.urls import patterns, url, include
+from django.conf.urls import url, include
from rest_framework import routers
-from quickstart import views
+from tutorial.quickstart import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
@@ -295,10 +285,10 @@ router.register(r'groups', views.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'))
-)
+]
Because we're using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class.
Again, if we need more control over the API URLs we can simply drop down to using regular class based views, and writing the URL conf explicitly.
@@ -345,6 +335,7 @@ REST_FRAMEWORK = {
Or directly through the browser...

+If you're working through the browser, make sure to login using the control in the top right corner.
Great, that was easy!
If you want to get a more in depth understanding of how REST framework fits together head on over to the tutorial, or start browsing the API guide.
--
cgit v1.2.3