aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/mixins.py
diff options
context:
space:
mode:
authorTom Christie2013-01-02 13:39:24 +0000
committerTom Christie2013-01-02 13:39:24 +0000
commitb807f3d52a68dbf657c6437f71ecbfcba0695972 (patch)
treeddd2f922ae3a92e833add9443ec4c60f458d0f9c /rest_framework/mixins.py
parenteff40391fb66f441d2f5a643d5d46f198cf77147 (diff)
downloaddjango-rest-framework-b807f3d52a68dbf657c6437f71ecbfcba0695972.tar.bz2
Keep API backwards compatible.
Diffstat (limited to 'rest_framework/mixins.py')
-rw-r--r--rest_framework/mixins.py21
1 files changed, 13 insertions, 8 deletions
diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py
index d828078d..43581ae9 100644
--- a/rest_framework/mixins.py
+++ b/rest_framework/mixins.py
@@ -16,11 +16,14 @@ class CreateModelMixin(object):
"""
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.DATA, files=request.FILES)
+
if serializer.is_valid():
self.pre_save(serializer.object)
self.object = serializer.save()
headers = self.get_success_headers(serializer.data)
- return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
+ return Response(serializer.data, status=status.HTTP_201_CREATED,
+ headers=headers)
+
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_success_headers(self, data):
@@ -81,21 +84,22 @@ class UpdateModelMixin(object):
Update a model instance.
Should be mixed in with `SingleObjectBaseView`.
"""
- def update(self, request, partial=False, *args, **kwargs):
+ def update(self, request, *args, **kwargs):
+ partial = kwargs.pop('partial', False)
try:
self.object = self.get_object()
- created = False
+ success_status_code = status.HTTP_200_OK
except Http404:
self.object = None
- created = True
+ success_status_code = status.HTTP_201_CREATED
- serializer = self.get_serializer(self.object, data=request.DATA, files=request.FILES, partial=partial)
+ serializer = self.get_serializer(self.object, data=request.DATA,
+ files=request.FILES, partial=partial)
if serializer.is_valid():
self.pre_save(serializer.object)
self.object = serializer.save()
- status_code = created and status.HTTP_201_CREATED or status.HTTP_200_OK
- return Response(serializer.data, status=status_code)
+ return Response(serializer.data, status=success_status_code)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@@ -115,7 +119,8 @@ class UpdateModelMixin(object):
# Ensure we clean the attributes so that we don't eg return integer
# pk using a string representation, as provided by the url conf kwarg.
- obj.full_clean()
+ if hasattr(obj, 'full_clean'):
+ obj.full_clean()
class DestroyModelMixin(object):