aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/mixins.py
diff options
context:
space:
mode:
authorBen Konrath2012-11-01 14:06:56 +0100
committerBen Konrath2012-11-01 14:06:56 +0100
commit9c82f9717e58f1bb250d5fd4b27619dbcbbd1f21 (patch)
treee976854e6871a8b826e91d8eb16d9a139b90664f /rest_framework/mixins.py
parentc24997df3b943e5d7a3b2e101508e4b79ee82dc4 (diff)
parent204db7bdaa59cd17f762d6cf0e6a8623c2cc9939 (diff)
downloaddjango-rest-framework-9c82f9717e58f1bb250d5fd4b27619dbcbbd1f21.tar.bz2
Merge branch 'master' into restframework2-filter
Diffstat (limited to 'rest_framework/mixins.py')
-rw-r--r--rest_framework/mixins.py25
1 files changed, 13 insertions, 12 deletions
diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py
index 04626fb0..b0cc043a 100644
--- a/rest_framework/mixins.py
+++ b/rest_framework/mixins.py
@@ -3,9 +3,6 @@ Basic building blocks for generic class based views.
We don't bind behaviour to http method handlers yet,
which allows mixin classes to be composed in interesting ways.
-
-Eg. Use mixins to build a Resource class, and have a Router class
- perform the binding of http methods to actions for us.
"""
from django.http import Http404
from rest_framework import status
@@ -20,10 +17,14 @@ class CreateModelMixin(object):
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.DATA)
if serializer.is_valid():
+ self.pre_save(serializer.object)
self.object = serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
+ def pre_save(self, obj):
+ pass
+
class ListModelMixin(object):
"""
@@ -46,7 +47,8 @@ class ListModelMixin(object):
# which may be `None` to disable pagination.
page_size = self.get_paginate_by(self.object_list)
if page_size:
- paginator, page, queryset, is_paginated = self.paginate_queryset(self.object_list, page_size)
+ packed = self.paginate_queryset(self.object_list, page_size)
+ paginator, page, queryset, is_paginated = packed
serializer = self.get_pagination_serializer(page)
else:
serializer = self.get_serializer(instance=self.object_list)
@@ -73,26 +75,25 @@ class UpdateModelMixin(object):
def update(self, request, *args, **kwargs):
try:
self.object = self.get_object()
+ success_status = status.HTTP_200_OK
except Http404:
self.object = None
+ success_status = status.HTTP_201_CREATED
serializer = self.get_serializer(data=request.DATA, instance=self.object)
if serializer.is_valid():
- if self.object is None:
- # If PUT occurs to a non existant object, we need to set any
- # attributes on the object that are implicit in the URL.
- self.update_urlconf_attributes(serializer.object)
+ self.pre_save(serializer.object)
self.object = serializer.save()
- return Response(serializer.data)
+ return Response(serializer.data, status=success_status)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
- def update_urlconf_attributes(self, obj):
+ def pre_save(self, obj):
"""
- When update (re)creates an object, we need to set any attributes that
- are tied to the URLconf.
+ Set any attributes on the object that are implicit in the request.
"""
+ # pk and/or slug attributes are implicit in the URL.
pk = self.kwargs.get(self.pk_url_kwarg, None)
if pk:
setattr(obj, 'pk', pk)