diff options
| author | Tom Christie | 2011-06-02 16:03:11 +0100 | 
|---|---|---|
| committer | Tom Christie | 2011-06-02 16:03:11 +0100 | 
| commit | 3531b0b35540ade216299c46717dcf9fa24487c7 (patch) | |
| tree | 813755ed96aa7a0476a719b570635424119f0000 /docs/examples | |
| parent | bf9ea978bca8928ba5726e4ec3d76e81d72aada8 (diff) | |
| download | django-rest-framework-3531b0b35540ade216299c46717dcf9fa24487c7.tar.bz2 | |
More updating docs for 0.2
--HG--
rename : docs/examples/modelresources.rst => docs/examples/modelviews.rst
rename : docs/examples/resources.rst => docs/examples/views.rst
Diffstat (limited to 'docs/examples')
| -rw-r--r-- | docs/examples/blogpost.rst | 22 | ||||
| -rw-r--r-- | docs/examples/modelviews.rst (renamed from docs/examples/modelresources.rst) | 22 | ||||
| -rw-r--r-- | docs/examples/views.rst (renamed from docs/examples/resources.rst) | 18 | 
3 files changed, 28 insertions, 34 deletions
diff --git a/docs/examples/blogpost.rst b/docs/examples/blogpost.rst index 9d762f52..36b9d982 100644 --- a/docs/examples/blogpost.rst +++ b/docs/examples/blogpost.rst @@ -8,26 +8,22 @@ Blog Posts API  The models  ---------- +In this example we're working from two related models: +  ``models.py``  .. include:: ../../examples/blogpost/models.py      :literal: -URL configuration ------------------ - -``urls.py`` - -.. include:: ../../examples/blogpost/urls.py -    :literal: -  Creating the resources  ---------------------- -Once we have some existing models there's very little we need to do to create the corresponding resources.  We simply create a base resource and an instance resource for each model we're working with. -django-rest-framework will provide the default operations on the resources all the usual input validation that Django's models can give us for free. +Once we have some existing models there's very little we need to do to create the API. +Firstly create a resource for each model that defines which fields we want to expose on the model. +Secondly we map a base view and an instance view for each resource. +The generic views :class:`.ListOrCreateModelView` and :class:`.InstanceModelView` provide default operations for listing, creating and updating our models via the API, and also automatically provide input validation using default ModelForms for each model. -#``views.py`` +``urls.py`` -#.. include:: ../../examples/blogpost/views.py -#   :literal:
\ No newline at end of file +.. include:: ../../examples/blogpost/urls.py +    :literal: diff --git a/docs/examples/modelresources.rst b/docs/examples/modelviews.rst index 375e4a8d..7cc78d39 100644 --- a/docs/examples/modelresources.rst +++ b/docs/examples/modelviews.rst @@ -1,7 +1,7 @@ -.. _modelresources: +.. _modelviews: -Getting Started - Model Resources ---------------------------------- +Getting Started - Model Views +-----------------------------  .. note:: @@ -15,21 +15,21 @@ Getting Started - Model Resources  Often you'll want parts of your API to directly map to existing django models.  Django REST framework handles this nicely for you in a couple of ways: -#. It automatically provides suitable create/read/update/delete methods for your resources. +#. It automatically provides suitable create/read/update/delete methods for your views.  #. Input validation occurs automatically, by using appropriate `ModelForms <http://docs.djangoproject.com/en/dev/topics/forms/modelforms/>`_. -We'll start of defining two resources in our urlconf again. +Here's the model we're working from in this example: -``urls.py`` +``models.py`` -.. include:: ../../examples/modelresourceexample/urls.py +.. include:: ../../examples/modelresourceexample/models.py      :literal: -Here's the models we're working from in this example.  It's usually a good idea to make sure you provide the :func:`get_absolute_url()` `permalink <http://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url>`_ for all models you want to expose via the API. +To add an API for the model, all we need to do is create a Resource for the model, and map a couple of views to it in our urlconf. -``models.py`` +``urls.py`` -.. include:: ../../examples/modelresourceexample/models.py +.. include:: ../../examples/modelresourceexample/urls.py      :literal:  And we're done.  We've now got a fully browseable API, which supports multiple input and output media types, and has all the nice automatic field validation that Django gives us for free. @@ -49,5 +49,3 @@ Or access it from the command line using curl:      #  Demonstrates API's input validation using JSON input      bash: curl -X POST -H 'Content-Type: application/json' --data-binary '{"foo":true}' http://api.django-rest-framework.org/model-resource-example/     {"detail": {"bar": ["This field is required."], "baz": ["This field is required."]}} - -We could also have added the handler methods :meth:`.Resource.get()`, :meth:`.Resource.post()` etc... seen in the last example, but Django REST framework provides nice default implementations for us that do exactly what we'd expect them to.  diff --git a/docs/examples/resources.rst b/docs/examples/views.rst index f3242421..59e13976 100644 --- a/docs/examples/resources.rst +++ b/docs/examples/views.rst @@ -1,7 +1,7 @@ -.. _resources: +.. _views: -Getting Started - Resources ---------------------------- +Getting Started - Views +-----------------------  .. note:: @@ -15,12 +15,12 @@ Getting Started - Resources  We're going to start off with a simple example, that demonstrates a few things: -#. Creating resources. -#. Linking resources. -#. Writing method handlers on resources. -#. Adding form validation to resources. +#. Creating views. +#. Linking views. +#. Writing method handlers on views. +#. Adding form validation to views. -First we'll define two resources in our urlconf. +First we'll define two views in our urlconf.  ``urls.py`` @@ -34,7 +34,7 @@ Now we'll add a form that we'll use for input validation.  This is completely op  .. include:: ../../examples/resourceexample/forms.py      :literal: -Now we'll write our resources.  The first is a read only resource that links to three instances of the second.  The second resource just has some stub handler methods to help us see that our example is working. +Now we'll write our views.  The first is a read only view that links to three instances of the second.  The second view just has some stub handler methods to help us see that our example is working.  ``views.py``  | 
