Skip to content

Commit

Permalink
Merge a091b98 into ab84df7
Browse files Browse the repository at this point in the history
  • Loading branch information
philratcliffe committed Mar 20, 2018
2 parents ab84df7 + a091b98 commit 20f536f
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions docs/rest-framework/getting_started.rst
Expand Up @@ -6,7 +6,7 @@ This tutorial is based on the Django REST Framework example and shows you how to

**NOTE**

The following code has been tested with django 1.7.7 and Django REST Framework 3.1.1
The following code has been tested with Django 2.0.3 and Django REST Framework 3.7.7

Step 1: Minimal setup
---------------------
Expand Down Expand Up @@ -48,19 +48,22 @@ Here's our project's root `urls.py` module:

.. code-block:: python
from django.conf.urls import url, include
from django.urls import path, include
from django.contrib.auth.models import User, Group
from django.contrib import admin
admin.autodiscover()
from rest_framework import permissions, routers, serializers, viewsets
from rest_framework import generics, permissions, serializers
from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope
# first we define the serializers
class UserSerializer(serializers.ModelSerializer):
class Meta:
class Metaclass UserList(generics.ListAPIView):
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
queryset = User.objects.all()
serializer_class = UserSerializer:
model = User
fields = ("username", "email", "first_name", "last_name", )
Expand All @@ -71,31 +74,27 @@ Here's our project's root `urls.py` module:
fields = ("name", )
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
queryset = User.objects.all()
serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated, TokenHasScope]
required_scopes = ['groups']
queryset = Group.objects.all()
serializer_class = GroupSerializer
# Create the API views
class UserList(generics.ListAPIView):
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
queryset = User.objects.all()
serializer_class = UserSerializer
# Routers provide an easy way of automatically determining the URL conf
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)
class GroupList(generics.ListAPIView):
permission_classes = [permissions.IsAuthenticated, TokenHasScope]
required_scopes = ['groups']
queryset = Group.objects.all()
serializer_class = GroupSerializer
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browseable API.
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
path('admin/', admin.site.urls),
path('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
path('users/', UserList.as_view()),
path('groups/', GroupList.as_view()),
# ...
]
Expand Down

0 comments on commit 20f536f

Please sign in to comment.