aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/mixins.py
diff options
context:
space:
mode:
authorTom Christie2013-05-17 22:09:23 +0100
committerTom Christie2013-05-17 22:09:23 +0100
commit34776da9249a5d73f822b3562bc56a5674b10ac7 (patch)
treea9423d6b5d7ae6a3a35b10517439b424f624bf55 /rest_framework/mixins.py
parentb6fb377c2b4b747597bc3291dadd52b633b135b4 (diff)
downloaddjango-rest-framework-34776da9249a5d73f822b3562bc56a5674b10ac7.tar.bz2
Minor mixin refactoring
Diffstat (limited to 'rest_framework/mixins.py')
-rw-r--r--rest_framework/mixins.py21
1 files changed, 11 insertions, 10 deletions
diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py
index 55d21a70..f3cd5868 100644
--- a/rest_framework/mixins.py
+++ b/rest_framework/mixins.py
@@ -43,7 +43,6 @@ def _get_validation_exclusions(obj, pk=None, slug_field=None, lookup_field=None)
class CreateModelMixin(object):
"""
Create a model instance.
- Should be mixed in with any `GenericAPIView`.
"""
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.DATA, files=request.FILES)
@@ -68,7 +67,6 @@ class CreateModelMixin(object):
class ListModelMixin(object):
"""
List a queryset.
- Should be mixed in with `MultipleObjectAPIView`.
"""
empty_error = "Empty list and '%(class_name)s.allow_empty' is False."
@@ -101,7 +99,6 @@ class ListModelMixin(object):
class RetrieveModelMixin(object):
"""
Retrieve a model instance.
- Should be mixed in with `SingleObjectAPIView`.
"""
def retrieve(self, request, *args, **kwargs):
self.object = self.get_object()
@@ -112,17 +109,22 @@ class RetrieveModelMixin(object):
class UpdateModelMixin(object):
"""
Update a model instance.
- Should be mixed in with `SingleObjectAPIView`.
"""
- def update(self, request, *args, **kwargs):
- partial = kwargs.pop('partial', False)
- self.object = None
+ def get_object_or_none(self):
try:
- self.object = self.get_object()
+ return self.get_object()
except Http404:
# If this is a PUT-as-create operation, we need to ensure that
# we have relevant permissions, as if this was a POST request.
- self.check_permissions(clone_request(request, 'POST'))
+ # This will either raise a PermissionDenied exception,
+ # or simply return None
+ self.check_permissions(clone_request(self.request, 'POST'))
+
+ def update(self, request, *args, **kwargs):
+ partial = kwargs.pop('partial', False)
+ self.object = self.get_object_or_none()
+
+ if self.object is None:
created = True
save_kwargs = {'force_insert': True}
success_status_code = status.HTTP_201_CREATED
@@ -175,7 +177,6 @@ class UpdateModelMixin(object):
class DestroyModelMixin(object):
"""
Destroy a model instance.
- Should be mixed in with `SingleObjectAPIView`.
"""
def destroy(self, request, *args, **kwargs):
obj = self.get_object()