You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generics.py 9.8KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. """
  2. Generic views that provide commonly needed behaviour.
  3. """
  4. from django.core.exceptions import ValidationError
  5. from django.db.models.query import QuerySet
  6. from django.http import Http404
  7. from django.shortcuts import get_object_or_404 as _get_object_or_404
  8. from rest_framework import mixins, views
  9. from rest_framework.settings import api_settings
  10. def get_object_or_404(queryset, *filter_args, **filter_kwargs):
  11. """
  12. Same as Django's standard shortcut, but make sure to also raise 404
  13. if the filter_kwargs don't match the required types.
  14. """
  15. try:
  16. return _get_object_or_404(queryset, *filter_args, **filter_kwargs)
  17. except (TypeError, ValueError, ValidationError):
  18. raise Http404
  19. class GenericAPIView(views.APIView):
  20. """
  21. Base class for all other generic views.
  22. """
  23. # You'll need to either set these attributes,
  24. # or override `get_queryset()`/`get_serializer_class()`.
  25. # If you are overriding a view method, it is important that you call
  26. # `get_queryset()` instead of accessing the `queryset` property directly,
  27. # as `queryset` will get evaluated only once, and those results are cached
  28. # for all subsequent requests.
  29. queryset = None
  30. serializer_class = None
  31. # If you want to use object lookups other than pk, set 'lookup_field'.
  32. # For more complex lookup requirements override `get_object()`.
  33. lookup_field = 'pk'
  34. lookup_url_kwarg = None
  35. # The filter backend classes to use for queryset filtering
  36. filter_backends = api_settings.DEFAULT_FILTER_BACKENDS
  37. # The style to use for queryset pagination.
  38. pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
  39. def get_queryset(self):
  40. """
  41. Get the list of items for this view.
  42. This must be an iterable, and may be a queryset.
  43. Defaults to using `self.queryset`.
  44. This method should always be used rather than accessing `self.queryset`
  45. directly, as `self.queryset` gets evaluated only once, and those results
  46. are cached for all subsequent requests.
  47. You may want to override this if you need to provide different
  48. querysets depending on the incoming request.
  49. (Eg. return a list of items that is specific to the user)
  50. """
  51. assert self.queryset is not None, (
  52. "'%s' should either include a `queryset` attribute, "
  53. "or override the `get_queryset()` method."
  54. % self.__class__.__name__
  55. )
  56. queryset = self.queryset
  57. if isinstance(queryset, QuerySet):
  58. # Ensure queryset is re-evaluated on each request.
  59. queryset = queryset.all()
  60. return queryset
  61. def get_object(self):
  62. """
  63. Returns the object the view is displaying.
  64. You may want to override this if you need to provide non-standard
  65. queryset lookups. Eg if objects are referenced using multiple
  66. keyword arguments in the url conf.
  67. """
  68. queryset = self.filter_queryset(self.get_queryset())
  69. # Perform the lookup filtering.
  70. lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
  71. assert lookup_url_kwarg in self.kwargs, (
  72. 'Expected view %s to be called with a URL keyword argument '
  73. 'named "%s". Fix your URL conf, or set the `.lookup_field` '
  74. 'attribute on the view correctly.' %
  75. (self.__class__.__name__, lookup_url_kwarg)
  76. )
  77. filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
  78. obj = get_object_or_404(queryset, **filter_kwargs)
  79. # May raise a permission denied
  80. self.check_object_permissions(self.request, obj)
  81. return obj
  82. def get_serializer(self, *args, **kwargs):
  83. """
  84. Return the serializer instance that should be used for validating and
  85. deserializing input, and for serializing output.
  86. """
  87. serializer_class = self.get_serializer_class()
  88. kwargs['context'] = self.get_serializer_context()
  89. return serializer_class(*args, **kwargs)
  90. def get_serializer_class(self):
  91. """
  92. Return the class to use for the serializer.
  93. Defaults to using `self.serializer_class`.
  94. You may want to override this if you need to provide different
  95. serializations depending on the incoming request.
  96. (Eg. admins get full serialization, others get basic serialization)
  97. """
  98. assert self.serializer_class is not None, (
  99. "'%s' should either include a `serializer_class` attribute, "
  100. "or override the `get_serializer_class()` method."
  101. % self.__class__.__name__
  102. )
  103. return self.serializer_class
  104. def get_serializer_context(self):
  105. """
  106. Extra context provided to the serializer class.
  107. """
  108. return {
  109. 'request': self.request,
  110. 'format': self.format_kwarg,
  111. 'view': self
  112. }
  113. def filter_queryset(self, queryset):
  114. """
  115. Given a queryset, filter it with whichever filter backend is in use.
  116. You are unlikely to want to override this method, although you may need
  117. to call it either from a list view, or from a custom `get_object`
  118. method if you want to apply the configured filtering backend to the
  119. default queryset.
  120. """
  121. for backend in list(self.filter_backends):
  122. queryset = backend().filter_queryset(self.request, queryset, self)
  123. return queryset
  124. @property
  125. def paginator(self):
  126. """
  127. The paginator instance associated with the view, or `None`.
  128. """
  129. if not hasattr(self, '_paginator'):
  130. if self.pagination_class is None:
  131. self._paginator = None
  132. else:
  133. self._paginator = self.pagination_class()
  134. return self._paginator
  135. def paginate_queryset(self, queryset):
  136. """
  137. Return a single page of results, or `None` if pagination is disabled.
  138. """
  139. if self.paginator is None:
  140. return None
  141. return self.paginator.paginate_queryset(queryset, self.request, view=self)
  142. def get_paginated_response(self, data):
  143. """
  144. Return a paginated style `Response` object for the given output data.
  145. """
  146. assert self.paginator is not None
  147. return self.paginator.get_paginated_response(data)
  148. # Concrete view classes that provide method handlers
  149. # by composing the mixin classes with the base view.
  150. class CreateAPIView(mixins.CreateModelMixin,
  151. GenericAPIView):
  152. """
  153. Concrete view for creating a model instance.
  154. """
  155. def post(self, request, *args, **kwargs):
  156. return self.create(request, *args, **kwargs)
  157. class ListAPIView(mixins.ListModelMixin,
  158. GenericAPIView):
  159. """
  160. Concrete view for listing a queryset.
  161. """
  162. def get(self, request, *args, **kwargs):
  163. return self.list(request, *args, **kwargs)
  164. class RetrieveAPIView(mixins.RetrieveModelMixin,
  165. GenericAPIView):
  166. """
  167. Concrete view for retrieving a model instance.
  168. """
  169. def get(self, request, *args, **kwargs):
  170. return self.retrieve(request, *args, **kwargs)
  171. class DestroyAPIView(mixins.DestroyModelMixin,
  172. GenericAPIView):
  173. """
  174. Concrete view for deleting a model instance.
  175. """
  176. def delete(self, request, *args, **kwargs):
  177. return self.destroy(request, *args, **kwargs)
  178. class UpdateAPIView(mixins.UpdateModelMixin,
  179. GenericAPIView):
  180. """
  181. Concrete view for updating a model instance.
  182. """
  183. def put(self, request, *args, **kwargs):
  184. return self.update(request, *args, **kwargs)
  185. def patch(self, request, *args, **kwargs):
  186. return self.partial_update(request, *args, **kwargs)
  187. class ListCreateAPIView(mixins.ListModelMixin,
  188. mixins.CreateModelMixin,
  189. GenericAPIView):
  190. """
  191. Concrete view for listing a queryset or creating a model instance.
  192. """
  193. def get(self, request, *args, **kwargs):
  194. return self.list(request, *args, **kwargs)
  195. def post(self, request, *args, **kwargs):
  196. return self.create(request, *args, **kwargs)
  197. class RetrieveUpdateAPIView(mixins.RetrieveModelMixin,
  198. mixins.UpdateModelMixin,
  199. GenericAPIView):
  200. """
  201. Concrete view for retrieving, updating a model instance.
  202. """
  203. def get(self, request, *args, **kwargs):
  204. return self.retrieve(request, *args, **kwargs)
  205. def put(self, request, *args, **kwargs):
  206. return self.update(request, *args, **kwargs)
  207. def patch(self, request, *args, **kwargs):
  208. return self.partial_update(request, *args, **kwargs)
  209. class RetrieveDestroyAPIView(mixins.RetrieveModelMixin,
  210. mixins.DestroyModelMixin,
  211. GenericAPIView):
  212. """
  213. Concrete view for retrieving or deleting a model instance.
  214. """
  215. def get(self, request, *args, **kwargs):
  216. return self.retrieve(request, *args, **kwargs)
  217. def delete(self, request, *args, **kwargs):
  218. return self.destroy(request, *args, **kwargs)
  219. class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
  220. mixins.UpdateModelMixin,
  221. mixins.DestroyModelMixin,
  222. GenericAPIView):
  223. """
  224. Concrete view for retrieving, updating or deleting a model instance.
  225. """
  226. def get(self, request, *args, **kwargs):
  227. return self.retrieve(request, *args, **kwargs)
  228. def put(self, request, *args, **kwargs):
  229. return self.update(request, *args, **kwargs)
  230. def patch(self, request, *args, **kwargs):
  231. return self.partial_update(request, *args, **kwargs)
  232. def delete(self, request, *args, **kwargs):
  233. return self.destroy(request, *args, **kwargs)