Getting started

Requirements

RIV does not require any additional third party modules. The serializers are built on top of the built-in Django serializers and therefore share the same requirements.

Installation

  1. Pull django-riv from GitHub

  2. Install using:

    sudo python setup.py install

    This also makes sure the required modules get installed automatically

Configuration

  1. Add RiV to the list of your installed apps:

    # settings.py
    INSTALLED_APPS = (
      # ...
      'riv'
    )
    
  2. Add the RivMiddleware to the end of your middleware classes:

    # settings.py
    MIDDLEWARE_CLASSES = (
      'django.middleware.common.CommonMiddleware',
      # ...
      'riv.middleware.RivMiddleware'
    )
    

#. The RiV serializers extend the builtin Django serializers. Add them to the list of SERIALIZATION_MODULES:

# settings.py
SERIALIZATION_MODULES = {
  'restjson': 'riv.serializers.json',
  'restxml':  'riv.serializers.xml'
}

#. For debugging purposes you can tell RIV to display errors. However, for security reasons this setting only has an effect if Django is running in debug mode:

# settings.py
RIV_DISPLAY_ERRORS = True

Creating resources

Add a resources.py file to your application and add a resource for each model. Each resource needs to set a wrapper class that links your views to the HTTP methods (will we create them in the next step):

# myapp/resources.py
from riv.resources import Resource
from myapp.models import MyModel, MyOtherModel
from myapp.wrappers import MyModelWrapper, MyOtherModelWrapper

class MyModelResource(Resource):
  _wrapper = MyModelWrapper()
  class Meta:
    name = 'mymodel'
    model = MyModel

class MyOtherModelResource(Resource):
  _wrapper = MyOtherModelWrapper()
  class Meta:
    name = 'myothermodel'
    model = MyOtherModel

Using the StandaloneWrapper

If you don’t any special data preparation in your views or if you simply don’t have views for your model you can use the StandaloneWrapper to make your model accessible directly:

# myapp/resources.py
from riv.resources import Resource
from riv.wrappers import StandaloneWrapper
from myapp.models import MyModel, MyOtherModel

class MyModelResource(Resource):
  _wrapper = StandaloneWrapper()
  class Meta:
    name = 'mymodel'
    model = MyModel

Setting up the API

The API class is used to bundle several resources together and form a logical unit. Adding the API to your URLconf makes the resources available:

# myapp/urls.py
from riv.api import Api
from myapp.resources import MyModelResource, MyOtherModelResource

# The api name is only used internally. It does not become part
# of your URL
myapi = Api(name='myapi')
myapi.register(MyModelResource())
myapi.register(MyOtherModelResource())

urlpatterns += patterns('',
  (r'api/', include(myapi.urls)),
)

Try it

You can now access the registered resources at:

http://<yourhost>/myapp/api/<resource_name>

In this case:

http://<yourhost>/myapp/api/mymodel
http://<yourhost>/myapp/api/myothermodel

Project Versions

Table Of Contents

Previous topic

Introduction

Next topic

Tutorial

This Page