Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Classes
4class REST(object):
5 """A utility class for collecting REST API information.
7 .. code-block:: python
9 # <app_name>/api/serializers.py
10 from rest_framework import serializers
11 from ..models import Category, Objective
13 class CategorySerializer(serializers.HyperlinkedModelSerializer):
15 class Meta:
16 model = Category
17 fields = (
18 "label",
19 "value",
20 )
22 class ObjectiveSerializer(serializers.HyperlinkedModelSerializer):
24 categories = serializers.HyperlinkedIdentityField(view_name="objectives-api-category-detail")
26 client = serializers.HyperlinkedIdentityField(view_name="clients-api-client-detail")
28 class Meta:
29 model = Objective
30 fields = (
31 "annual_resolved_value",
32 "cached_percentage_complete",
33 "categories",
34 "client",
35 "description",
36 "end_date",
37 "percentage_complete_override",
38 "priority",
39 "stage",
40 "start_date",
41 "title",
42 )
44 # <app_name>/api/views.py
45 from rest_framework import viewsets
46 from ..models import Category, Objective
47 from .serializers import CategorySerializer, ObjectiveSerializer
50 class CategoryViewSet(viewsets.ModelViewSet):
51 queryset = Category.objects.all()
52 serializer_class = CategorySerializer
55 class ObjectiveViewSet(viewsets.ModelViewSet):
56 queryset = Objective.objects.all()
57 serializer_class = ObjectiveSerializer
59 .. code-block:: python
61 # <app_name>/api/__init__.py
62 from superdjango.interfaces.reset import api, REST
63 from .views import CategoryViewSet, ObjectiveViewSet
65 class ObjectiveREST(REST):
66 # basename = "objectives-api"
67 # prefix = "objectives"
68 viewsets = [
69 CategoryViewSet,
70 ObjectiveViewSet,
71 ]
74 api.register("objectives", ObjectiveREST)
76 ``basename`` and ``prefix`` are optional and default to ``<app_name>-api`` and ``<app_name>`` respectively. In the
77 example above, category detail is available at ``objectives-api-category-detail`` and objective detail at
78 ``objectives-api-objective-detail``.
80 """
81 basename = None
82 prefix = None
83 version = None
84 viewsets = list()