mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-09-16 17:22:32 +03:00
PUT returns a 201 status when instance was created.
Note: This behavior is still idempotent, as the state of the system is the same after multiple PUT requests as it is after one. However, it is helpful to know whether an instance was created or whether it already existed.
This commit is contained in:
parent
912a897e2d
commit
3cef6bd02c
|
@ -181,7 +181,7 @@ class RequestMixin(object):
|
|||
return parser.parse(stream)
|
||||
|
||||
raise ErrorResponse(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
||||
{'detail': 'Unsupported media type in request \'%s\'.' %
|
||||
{'detail': 'Unsupported media type in request \'%s\'.' %
|
||||
content_type})
|
||||
|
||||
@property
|
||||
|
@ -522,14 +522,14 @@ class ExistingInstanceMixin (object):
|
|||
"""
|
||||
Assume a single instance for the view. Caches the instance object on self.
|
||||
"""
|
||||
|
||||
|
||||
def get_instance(self):
|
||||
if not hasattr(self, 'model_instance'):
|
||||
query_kwargs = self.get_query_kwargs(
|
||||
self.request, *self.args, **self.kwargs)
|
||||
self.model_instance = self.get_queryset().get(**query_kwargs)
|
||||
return self.model_instance
|
||||
|
||||
|
||||
def get_instance_or_404(self):
|
||||
model = self.resource.model
|
||||
|
||||
|
@ -538,7 +538,7 @@ class ExistingInstanceMixin (object):
|
|||
except model.DoesNotExist:
|
||||
raise ErrorResponse(status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
|
||||
class ReadModelMixin(ModelMixin, ExistingInstanceMixin):
|
||||
"""
|
||||
Behavior to read a `model` instance on GET requests
|
||||
|
@ -602,10 +602,13 @@ class UpdateModelMixin(ModelMixin, ExistingInstanceMixin):
|
|||
|
||||
for (key, val) in self.CONTENT.items():
|
||||
setattr(self.model_instance, key, val)
|
||||
|
||||
self.model_instance.save()
|
||||
return self.model_instance
|
||||
except model.DoesNotExist:
|
||||
self.model_instance = model(**self.get_instance_data(model, self.CONTENT, *args, **kwargs))
|
||||
self.model_instance.save()
|
||||
return self.model_instance
|
||||
self.model_instance.save()
|
||||
return Response(status.HTTP_201_CREATED, self.model_instance)
|
||||
|
||||
|
||||
class DeleteModelMixin(ModelMixin, ExistingInstanceMixin):
|
||||
|
|
Loading…
Reference in New Issue
Block a user