mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Keep API backwards compatible.
This commit is contained in:
parent
eff40391fb
commit
b807f3d52a
|
@ -47,14 +47,16 @@ class GenericAPIView(views.APIView):
|
|||
|
||||
return serializer_class
|
||||
|
||||
def get_serializer(self, instance=None, data=None, files=None, partial=False):
|
||||
def get_serializer(self, instance=None, data=None,
|
||||
files=None, partial=False):
|
||||
"""
|
||||
Return the serializer instance that should be used for validating and
|
||||
deserializing input, and for serializing output.
|
||||
"""
|
||||
serializer_class = self.get_serializer_class()
|
||||
context = self.get_serializer_context()
|
||||
return serializer_class(instance, data=data, files=files, partial=partial, context=context)
|
||||
return serializer_class(instance, data=data, files=files,
|
||||
partial=partial, context=context)
|
||||
|
||||
|
||||
class MultipleObjectAPIView(MultipleObjectMixin, GenericAPIView):
|
||||
|
@ -169,10 +171,11 @@ class UpdateAPIView(mixins.UpdateModelMixin,
|
|||
Concrete view for updating a model instance.
|
||||
"""
|
||||
def put(self, request, *args, **kwargs):
|
||||
return self.update(request, partial=False, *args, **kwargs)
|
||||
return self.update(request, *args, **kwargs)
|
||||
|
||||
def patch(self, request, *args, **kwargs):
|
||||
return self.update(request, partial=True, *args, **kwargs)
|
||||
kwargs['partial'] = True
|
||||
return self.update(request, *args, **kwargs)
|
||||
|
||||
|
||||
class ListCreateAPIView(mixins.ListModelMixin,
|
||||
|
@ -212,10 +215,11 @@ class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
|
|||
return self.retrieve(request, *args, **kwargs)
|
||||
|
||||
def put(self, request, *args, **kwargs):
|
||||
return self.update(request, partial=False, *args, **kwargs)
|
||||
return self.update(request, *args, **kwargs)
|
||||
|
||||
def delete(self, request, *args, **kwargs):
|
||||
return self.destroy(request, *args, **kwargs)
|
||||
|
||||
def patch(self, request, *args, **kwargs):
|
||||
return self.update(request, partial=True, *args, **kwargs)
|
||||
kwargs['partial'] = True
|
||||
return self.update(request, *args, **kwargs)
|
||||
|
|
|
@ -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,6 +119,7 @@ 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.
|
||||
if hasattr(obj, 'full_clean'):
|
||||
obj.full_clean()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user