Skip to content

Commit

Permalink
PUT as create should return 201. Fixes #340.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Oct 31, 2012
1 parent 3a99170 commit 027c907
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/topics/release-notes.md
Expand Up @@ -4,6 +4,10 @@
>
> — Eric S. Raymond, [The Cathedral and the Bazaar][cite].
## Master

* If PUT creates an instance return '201 Created', instead of '200 OK'.

## 2.0.0

* **Fix all of the things.** (Well, almost.)
Expand Down
7 changes: 3 additions & 4 deletions rest_framework/mixins.py
Expand Up @@ -3,9 +3,6 @@
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
Expand Down Expand Up @@ -78,15 +75,17 @@ 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():
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)

Expand Down
6 changes: 3 additions & 3 deletions rest_framework/tests/generics.py
Expand Up @@ -236,7 +236,7 @@ def test_put_to_deleted_instance(self):
request = factory.put('/1', json.dumps(content),
content_type='application/json')
response = self.view(request, pk=1).render()
self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals(response.status_code, status.HTTP_201_CREATED)
self.assertEquals(response.data, {'id': 1, 'text': 'foobar'})
updated = self.objects.get(id=1)
self.assertEquals(updated.text, 'foobar')
Expand All @@ -251,7 +251,7 @@ def test_put_as_create_on_id_based_url(self):
request = factory.put('/5', json.dumps(content),
content_type='application/json')
response = self.view(request, pk=5).render()
self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals(response.status_code, status.HTTP_201_CREATED)
new_obj = self.objects.get(pk=5)
self.assertEquals(new_obj.text, 'foobar')

Expand All @@ -264,7 +264,7 @@ def test_put_as_create_on_slug_based_url(self):
request = factory.put('/test_slug', json.dumps(content),
content_type='application/json')
response = self.slug_based_view(request, slug='test_slug').render()
self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals(response.status_code, status.HTTP_201_CREATED)
self.assertEquals(response.data, {'slug': 'test_slug', 'text': 'foobar'})
new_obj = SlugBasedModel.objects.get(slug='test_slug')
self.assertEquals(new_obj.text, 'foobar')
Expand Down

2 comments on commit 027c907

@markotibold
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) I was just about to pull the latest changes to fix this one.

Ya planning to keep doing development on the master branche from now ons?

@tomchristie
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup - I'll remove the restframework2 branch now.

Please sign in to comment.