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.
Pull django-riv from GitHub
Install using:
sudo python setup.py install
This also makes sure the required modules get installed automatically
Add RiV to the list of your installed apps:
# settings.py
INSTALLED_APPS = (
# ...
'riv'
)
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
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
A wrapper should be derived from the BaseWrapper class. The wrapper can define the following methods:
You can link to your existing views using the call_view method. Consider you have the following view:
# myapp/views.py
def mymodel_detail(request, id):
m = get_object_or_404(MyModel, pk=id)
return render_to_response('detail.html', {'mymodel': m})
Your wrapper class should look like this:
# myapp/wrappers.py
from riv.wrappers import BaseWrapper
from riv.helpers import call_view
from myapp import views
class MyModelWrapper(BaseWrapper):
read = call_view(views.mymodel_detail)
Now you need adjust your view to handle calls through the API properly:
# myapp/views.py
...
from riv.shortcuts import render_to_rest
def mymodel_detail(request, id):
m = get_object_or_404(MyModel, pk=id)
if request.is_rest():
return render_to_rest(m)
return render_to_response('detail.html', {'mymodel': m})
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
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)),
)
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