Quick Start

Nap REST views work by combining Mappers with composible Class-Based Views.

Let’s see how you might got about providng an API for the Poll example from the Django tutorial.

Mapper/Views Quick Start

  1. Create a Mapper for your Model in mappers.py

This is very much like defining a ModelForm.

from nap import mapper

from . import models


class QuestionMapper(mapper.ModelMapper):
    class Meta:
        model = models.Question
        fields = '__all__'
  1. Create some views in rest_views.py
from nap.rest import views

from . import mappers, models

class QuestionMixin:
    model = models.Question
    mapper_class = mappers.QuestionMapper


class QuestionListView(QuestionMixin,
                       views.ListGetMixin,
                       views.ListPostMixin,
                       views.ListBaseView):
    pass

class QuestionObjectView(QuestionMixin,
                         views.ObjectGetMixin,
                         views.ObjectPutMixin,
                         views.ObjectBaseView):
    pass

The ListBaseView provides the core of any object list view, deriving from Django’s MultipleObjectMixin. Then we mix in the default handlers for GET and POST actions.

Similarly, the ObjectBaseView supports single object access, deriving from Django’s SingleObjectMixin.

Where the list view has POST to create a new record, the object view has PUT to update an existing record.

  1. Add your APIs to your URLs:
urlpatterns = [
    url(r'^question/$',
        QuestionListView.as_view(),
        name='question-list'),

    url(r'^question/(?P<pk>\d+)/$',
        QuestionObjectView.as_view(),
        name='question-detail'),
]

And we’re done. You can now access your Question model!