aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/mixins.py
diff options
context:
space:
mode:
authorTom Christie2012-10-25 12:15:31 +0100
committerTom Christie2012-10-25 12:15:31 +0100
commit8c360770c18ac38a2f4da81a3553fb40592558c4 (patch)
treee790e90e8dcc3d9ca0de552ad80e412f3fcced4e /rest_framework/mixins.py
parent3e751ccd8aa2870c125a17de6af6e1909aa2b35e (diff)
downloaddjango-rest-framework-8c360770c18ac38a2f4da81a3553fb40592558c4.tar.bz2
Add pre_save hook in generic views
Diffstat (limited to 'rest_framework/mixins.py')
-rw-r--r--rest_framework/mixins.py18
1 files changed, 10 insertions, 8 deletions
diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py
index 29153e18..8873e4ae 100644
--- a/rest_framework/mixins.py
+++ b/rest_framework/mixins.py
@@ -20,10 +20,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 +50,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)
@@ -79,20 +84,17 @@ class UpdateModelMixin(object):
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.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)