mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 04:07:39 +03:00
Merge pull request #320 from j4mie/split-generic-views
Rename generic base classes, and add CreateAPIView, DeleteAPIView, UpdateAPIView.
This commit is contained in:
commit
e987737621
|
@ -49,21 +49,21 @@ For very simple cases you might want to pass through any class attributes using
|
|||
|
||||
The following classes are the concrete generic views. If you're using generic views this is normally the level you'll be working at unless you need heavily customized behavior.
|
||||
|
||||
## CreateAPIView
|
||||
|
||||
Used for **create-only** endpoints.
|
||||
|
||||
Provides `post` method handlers.
|
||||
|
||||
Extends: [GenericAPIView], [CreateModelMixin]
|
||||
|
||||
## ListAPIView
|
||||
|
||||
Used for **read-only** endpoints to represent a **collection of model instances**.
|
||||
|
||||
Provides a `get` method handler.
|
||||
|
||||
Extends: [MultipleObjectBaseAPIView], [ListModelMixin]
|
||||
|
||||
## ListCreateAPIView
|
||||
|
||||
Used for **read-write** endpoints to represent a **collection of model instances**.
|
||||
|
||||
Provides `get` and `post` method handlers.
|
||||
|
||||
Extends: [MultipleObjectBaseAPIView], [ListModelMixin], [CreateModelMixin]
|
||||
Extends: [MultipleObjectAPIView], [ListModelMixin]
|
||||
|
||||
## RetrieveAPIView
|
||||
|
||||
|
@ -71,7 +71,31 @@ Used for **read-only** endpoints to represent a **single model instance**.
|
|||
|
||||
Provides a `get` method handler.
|
||||
|
||||
Extends: [SingleObjectBaseAPIView], [RetrieveModelMixin]
|
||||
Extends: [SingleObjectAPIView], [RetrieveModelMixin]
|
||||
|
||||
## DestroyAPIView
|
||||
|
||||
Used for **delete-only** endpoints for a **single model instance**.
|
||||
|
||||
Provides a `delete` method handler.
|
||||
|
||||
Extends: [SingleObjectAPIView], [DestroyModelMixin]
|
||||
|
||||
## UpdateAPIView
|
||||
|
||||
Used for **update-only** endpoints for a **single model instance**.
|
||||
|
||||
Provides a `put` method handler.
|
||||
|
||||
Extends: [SingleObjectAPIView], [UpdateModelMixin]
|
||||
|
||||
## ListCreateAPIView
|
||||
|
||||
Used for **read-write** endpoints to represent a **collection of model instances**.
|
||||
|
||||
Provides `get` and `post` method handlers.
|
||||
|
||||
Extends: [MultipleObjectAPIView], [ListModelMixin], [CreateModelMixin]
|
||||
|
||||
## RetrieveDestroyAPIView
|
||||
|
||||
|
@ -79,15 +103,15 @@ Used for **read or delete** endpoints to represent a **single model instance**.
|
|||
|
||||
Provides `get` and `delete` method handlers.
|
||||
|
||||
Extends: [SingleObjectBaseAPIView], [RetrieveModelMixin], [DestroyModelMixin]
|
||||
Extends: [SingleObjectAPIView], [RetrieveModelMixin], [DestroyModelMixin]
|
||||
|
||||
## RetrieveUpdateDestroyAPIView
|
||||
|
||||
Used for **read-write** endpoints to represent a **single model instance**.
|
||||
Used for **read-write-delete** endpoints to represent a **single model instance**.
|
||||
|
||||
Provides `get`, `put` and `delete` method handlers.
|
||||
|
||||
Extends: [SingleObjectBaseAPIView], [RetrieveModelMixin], [UpdateModelMixin], [DestroyModelMixin]
|
||||
Extends: [SingleObjectAPIView], [RetrieveModelMixin], [UpdateModelMixin], [DestroyModelMixin]
|
||||
|
||||
---
|
||||
|
||||
|
@ -95,17 +119,17 @@ Extends: [SingleObjectBaseAPIView], [RetrieveModelMixin], [UpdateModelMixin], [D
|
|||
|
||||
Each of the generic views provided is built by combining one of the base views below, with one or more mixin classes.
|
||||
|
||||
## BaseAPIView
|
||||
## GenericAPIView
|
||||
|
||||
Extends REST framework's `APIView` class, adding support for serialization of model instances and model querysets.
|
||||
|
||||
## MultipleObjectBaseAPIView
|
||||
## MultipleObjectAPIView
|
||||
|
||||
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
|
||||
## SingleObjectAPIView
|
||||
|
||||
Provides a base view for acting on a single object, by combining REST framework's `APIView`, and Django's [SingleObjectMixin].
|
||||
|
||||
|
@ -121,31 +145,31 @@ The mixin classes provide the actions that are used to provide the basic view be
|
|||
|
||||
Provides a `.list(request, *args, **kwargs)` method, that implements listing a queryset.
|
||||
|
||||
Should be mixed in with [MultipleObjectBaseAPIView].
|
||||
Should be mixed in with [MultipleObjectAPIView].
|
||||
|
||||
## CreateModelMixin
|
||||
|
||||
Provides a `.create(request, *args, **kwargs)` method, that implements creating and saving a new model instance.
|
||||
|
||||
Should be mixed in with any [BaseAPIView].
|
||||
Should be mixed in with any [GenericAPIView].
|
||||
|
||||
## RetrieveModelMixin
|
||||
|
||||
Provides a `.retrieve(request, *args, **kwargs)` method, that implements returning an existing model instance in a response.
|
||||
|
||||
Should be mixed in with [SingleObjectBaseAPIView].
|
||||
Should be mixed in with [SingleObjectAPIView].
|
||||
|
||||
## UpdateModelMixin
|
||||
|
||||
Provides a `.update(request, *args, **kwargs)` method, that implements updating and saving an existing model instance.
|
||||
|
||||
Should be mixed in with [SingleObjectBaseAPIView].
|
||||
Should be mixed in with [SingleObjectAPIView].
|
||||
|
||||
## DestroyModelMixin
|
||||
|
||||
Provides a `.destroy(request, *args, **kwargs)` method, that implements deletion of an existing model instance.
|
||||
|
||||
Should be mixed in with [SingleObjectBaseAPIView].
|
||||
Should be mixed in with [SingleObjectAPIView].
|
||||
|
||||
[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/
|
||||
|
@ -153,9 +177,9 @@ Should be mixed in with [SingleObjectBaseAPIView].
|
|||
[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/
|
||||
|
||||
[BaseAPIView]: #baseapiview
|
||||
[SingleObjectBaseAPIView]: #singleobjectbaseapiview
|
||||
[MultipleObjectBaseAPIView]: #multipleobjectbaseapiview
|
||||
[GenericAPIView]: #genericapiview
|
||||
[SingleObjectAPIView]: #singleobjectapiview
|
||||
[MultipleObjectAPIView]: #multipleobjectapiview
|
||||
[ListModelMixin]: #listmodelmixin
|
||||
[CreateModelMixin]: #createmodelmixin
|
||||
[RetrieveModelMixin]: #retrievemodelmixin
|
||||
|
|
|
@ -10,7 +10,7 @@ from django.views.generic.list import MultipleObjectMixin
|
|||
|
||||
### Base classes for the generic views ###
|
||||
|
||||
class BaseView(views.APIView):
|
||||
class GenericAPIView(views.APIView):
|
||||
"""
|
||||
Base class for all other generic views.
|
||||
"""
|
||||
|
@ -51,7 +51,7 @@ class BaseView(views.APIView):
|
|||
return serializer_class(data, instance=instance, context=context)
|
||||
|
||||
|
||||
class MultipleObjectBaseView(MultipleObjectMixin, BaseView):
|
||||
class MultipleObjectAPIView(MultipleObjectMixin, GenericAPIView):
|
||||
"""
|
||||
Base class for generic views onto a queryset.
|
||||
"""
|
||||
|
@ -75,7 +75,7 @@ class MultipleObjectBaseView(MultipleObjectMixin, BaseView):
|
|||
return pagination_serializer_class(instance=page, context=context)
|
||||
|
||||
|
||||
class SingleObjectBaseView(SingleObjectMixin, BaseView):
|
||||
class SingleObjectAPIView(SingleObjectMixin, GenericAPIView):
|
||||
"""
|
||||
Base class for generic views onto a model instance.
|
||||
"""
|
||||
|
@ -86,7 +86,7 @@ class SingleObjectBaseView(SingleObjectMixin, BaseView):
|
|||
"""
|
||||
Override default to add support for object-level permissions.
|
||||
"""
|
||||
obj = super(SingleObjectBaseView, self).get_object()
|
||||
obj = super(SingleObjectAPIView, self).get_object()
|
||||
if not self.has_permission(self.request, obj):
|
||||
self.permission_denied(self.request)
|
||||
return obj
|
||||
|
@ -95,8 +95,19 @@ class SingleObjectBaseView(SingleObjectMixin, BaseView):
|
|||
### Concrete view classes that provide method handlers ###
|
||||
### by composing the mixin classes with a base view. ###
|
||||
|
||||
|
||||
class CreateAPIView(mixins.CreateModelMixin,
|
||||
GenericAPIView):
|
||||
|
||||
"""
|
||||
Concrete view for creating a model instance.
|
||||
"""
|
||||
def post(self, request, *args, **kwargs):
|
||||
return self.create(request, *args, **kwargs)
|
||||
|
||||
|
||||
class ListAPIView(mixins.ListModelMixin,
|
||||
MultipleObjectBaseView):
|
||||
MultipleObjectAPIView):
|
||||
"""
|
||||
Concrete view for listing a queryset.
|
||||
"""
|
||||
|
@ -104,9 +115,38 @@ class ListAPIView(mixins.ListModelMixin,
|
|||
return self.list(request, *args, **kwargs)
|
||||
|
||||
|
||||
class RetrieveAPIView(mixins.RetrieveModelMixin,
|
||||
SingleObjectAPIView):
|
||||
"""
|
||||
Concrete view for retrieving a model instance.
|
||||
"""
|
||||
def get(self, request, *args, **kwargs):
|
||||
return self.retrieve(request, *args, **kwargs)
|
||||
|
||||
|
||||
class DestroyAPIView(mixins.DestroyModelMixin,
|
||||
SingleObjectAPIView):
|
||||
|
||||
"""
|
||||
Concrete view for deleting a model instance.
|
||||
"""
|
||||
def delete(self, request, *args, **kwargs):
|
||||
return self.destroy(request, *args, **kwargs)
|
||||
|
||||
|
||||
class UpdateAPIView(mixins.UpdateModelMixin,
|
||||
SingleObjectAPIView):
|
||||
|
||||
"""
|
||||
Concrete view for updating a model instance.
|
||||
"""
|
||||
def put(self, request, *args, **kwargs):
|
||||
return self.update(request, *args, **kwargs)
|
||||
|
||||
|
||||
class ListCreateAPIView(mixins.ListModelMixin,
|
||||
mixins.CreateModelMixin,
|
||||
MultipleObjectBaseView):
|
||||
MultipleObjectAPIView):
|
||||
"""
|
||||
Concrete view for listing a queryset or creating a model instance.
|
||||
"""
|
||||
|
@ -117,18 +157,9 @@ class ListCreateAPIView(mixins.ListModelMixin,
|
|||
return self.create(request, *args, **kwargs)
|
||||
|
||||
|
||||
class RetrieveAPIView(mixins.RetrieveModelMixin,
|
||||
SingleObjectBaseView):
|
||||
"""
|
||||
Concrete view for retrieving a model instance.
|
||||
"""
|
||||
def get(self, request, *args, **kwargs):
|
||||
return self.retrieve(request, *args, **kwargs)
|
||||
|
||||
|
||||
class RetrieveDestroyAPIView(mixins.RetrieveModelMixin,
|
||||
mixins.DestroyModelMixin,
|
||||
SingleObjectBaseView):
|
||||
SingleObjectAPIView):
|
||||
"""
|
||||
Concrete view for retrieving or deleting a model instance.
|
||||
"""
|
||||
|
@ -142,7 +173,7 @@ class RetrieveDestroyAPIView(mixins.RetrieveModelMixin,
|
|||
class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
|
||||
mixins.UpdateModelMixin,
|
||||
mixins.DestroyModelMixin,
|
||||
SingleObjectBaseView):
|
||||
SingleObjectAPIView):
|
||||
"""
|
||||
Concrete view for retrieving, updating or deleting a model instance.
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user