aboutsummaryrefslogtreecommitdiffstats
path: root/docs/api-guide/generic-views.md
blob: 88f245c7a61181ec343e98fd286125fe5140fc0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<a class="github" href="mixins.py"></a>
<a class="github" href="generics.py"></a>

# Generic views

> Django’s generic views... were developed as a shortcut for common usage patterns... They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself.
>
> &mdash; [Django Documentation][cite]

One of the key benefits of class based views is the way they allow you to compose bits of reusable behaviour.  REST framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns. 

## Example

...

---

# API Reference

## ListAPIView

Used for read-write endpoints to represent a collection of model instances.

Provides a `get` method handler.

## ListCreateAPIView

Used for read-write endpoints to represent a collection of model instances.

Provides `get` and `post` method handlers.

## RetrieveAPIView

Used for read-only endpoints to represent a single model instance.

Provides a `get` method handler.

## RetrieveUpdateDestroyAPIView

Used for read-write endpoints to represent a single model instance.

Provides `get`, `put` and `delete` method handlers.

---

# Base views

## BaseAPIView

Extends REST framework's `APIView` class, adding support for serialization of model instances and model querysets.

## MultipleObjectBaseAPIView

Provides a base view for acting on a single object, by combining REST framework's `APIView`, and Django's [MultipleObjectMixin].

**See also:** ccbv.co.uk documentation for [MultipleObjectMixin][multiple-object-mixin-classy].

## SingleObjectBaseAPIView

Provides a base view for acting on a single object, by combining REST framework's `APIView`, and Django's [SingleObjectMixin].

**See also:** ccbv.co.uk documentation for [SingleObjectMixin][single-object-mixin-classy].

---

# Mixins

The mixin classes provide the actions that are used 

## ListModelMixin

Provides a `.list(request, *args, **kwargs)` method, that implements listing a queryset.

## CreateModelMixin

Provides a `.create(request, *args, **kwargs)` method, that implements creating and saving a new model instance.

## RetrieveModelMixin

Provides a `.retrieve(request, *args, **kwargs)` method, that implements returning an existing model instance in a response.

## UpdateModelMixin

Provides a `.update(request, *args, **kwargs)` method, that implements updating and saving an existing model instance.

## DestroyModelMixin

Provides a `.destroy(request, *args, **kwargs)` method, that implements deletion of an existing model instance.

## MetadataMixin

Provides a `.metadata(request, *args, **kwargs)` method, that returns a response containing metadata about the view.

[cite]: https://docs.djangoproject.com/en/dev/ref/class-based-views/#base-vs-generic-views
[MultipleObjectMixin]: https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-multiple-object/
[SingleObjectMixin]: https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-single-object/
[multiple-object-mixin-classy]: http://ccbv.co.uk/projects/Django/1.4/django.views.generic.list/MultipleObjectMixin/
[single-object-mixin-classy]: http://ccbv.co.uk/projects/Django/1.4/django.views.generic.detail/SingleObjectMixin/