diff options
| author | Tom Christie | 2013-04-24 22:40:24 +0100 | 
|---|---|---|
| committer | Tom Christie | 2013-04-24 22:40:24 +0100 | 
| commit | b94da2468cdda6b0ad491574d35097d0e336ea7f (patch) | |
| tree | bfb05f85703a738918252c8968f9a460554cae3f /docs/index.md | |
| parent | 835d3f89d37b873b2ef96dc7d71922b035b07328 (diff) | |
| download | django-rest-framework-b94da2468cdda6b0ad491574d35097d0e336ea7f.tar.bz2 | |
Various clean up and lots of docs
Diffstat (limited to 'docs/index.md')
| -rw-r--r-- | docs/index.md | 48 | 
1 files changed, 48 insertions, 0 deletions
| diff --git a/docs/index.md b/docs/index.md index d51bbe13..f3abeb6b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -73,6 +73,54 @@ If you're intending to use the browseable API you'll probably also want to add R  Note that the URL path can be whatever you want, but you must include `'rest_framework.urls'` with the `'rest_framework'` namespace. +## Example + +Let's take a look at a quick example of using REST framework to build a simple model-backed API. + +We'll create a read-write API for accessing users and groups. + +    from django.conf.urls.defaults import url, patterns, include +    from django.contrib.auth.models import User, Group +    from rest_framework import serializers, viewsets, routers + + +    # Serializers control the representations your API exposes. +    class UserSerializer(serializers.HyperlinkedModelSerializer): +        class Meta: +            model = User +            fields = ('url', 'email', 'is_staff', 'groups') + + +    class GroupSerializer(serializers.HyperlinkedModelSerializer): +        class Meta: +            model = Group +            fields = ('url', 'name') + + +    # ViewSets define the view behavior. +    class UserViewSet(viewsets.ModelViewSet): +        queryset = User.objects.all() +        serializer_class = UserSerializer + + +    class GroupViewSet(viewsets.ModelViewSet): +        queryset = Group.objects.all() +        serializer_class = GroupSerializer + + +    # Routers provide a convienient way of automatically managing your URLs. +    router = routers.DefaultRouter() +    router.register(r'users', UserViewSet, 'user') +    router.register(r'groups', GroupViewSet, 'group') + + +    # Wire up our API URLs, letting the router do the hard work. +    # Additionally, we include login URLs for the browseable API. +    urlpatterns = patterns('', +        url(r'^', include(router.urls)), +        url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) +    ) +  ## Quickstart  Can't wait to get started?  The [quickstart guide][quickstart] is the fastest way to get up and running, and building APIs with REST framework. | 
