mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
Support PUT for create
This commit is contained in:
parent
7218bcbade
commit
2533c2452b
|
@ -71,13 +71,38 @@ class UpdateModelMixin(object):
|
|||
Should be mixed in with `SingleObjectBaseView`.
|
||||
"""
|
||||
def update(self, request, *args, **kwargs):
|
||||
try:
|
||||
self.object = self.get_object()
|
||||
except Http404:
|
||||
self.object = None
|
||||
|
||||
serializer = self.get_serializer(data=request.DATA, instance=self.object)
|
||||
if serializer.is_valid():
|
||||
if self.object is None:
|
||||
obj = serializer.object
|
||||
# TODO: Make ModelSerializers return regular instances,
|
||||
# not DeserializedObject
|
||||
if hasattr(obj, 'object'):
|
||||
obj = obj.object
|
||||
self.update_urlconf_attributes(serializer.object.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):
|
||||
"""
|
||||
When update (re)creates an object, we need to set any attributes that
|
||||
are tied to the URLconf.
|
||||
"""
|
||||
pk = self.kwargs.get(self.pk_url_kwarg, None)
|
||||
if pk:
|
||||
setattr(obj, 'pk', pk)
|
||||
|
||||
slug = self.kwargs.get(self.slug_url_kwarg, None)
|
||||
if slug:
|
||||
slug_field = self.get_slug_field()
|
||||
setattr(obj, slug_field, slug)
|
||||
|
||||
|
||||
class DestroyModelMixin(object):
|
||||
"""
|
||||
|
|
|
@ -208,3 +208,18 @@ class TestInstanceView(TestCase):
|
|||
self.assertEquals(response.data, {'id': 1, 'text': 'foobar'})
|
||||
updated = self.objects.get(id=1)
|
||||
self.assertEquals(updated.text, 'foobar')
|
||||
|
||||
def test_put_to_deleted_instance(self):
|
||||
"""
|
||||
PUT requests to RetrieveUpdateDestroyAPIView should create an object
|
||||
if it does not currently exist.
|
||||
"""
|
||||
self.objects.get(id=1).delete()
|
||||
content = {'text': 'foobar'}
|
||||
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.data, {'id': 1, 'text': 'foobar'})
|
||||
updated = self.objects.get(id=1)
|
||||
self.assertEquals(updated.text, 'foobar')
|
||||
|
|
Loading…
Reference in New Issue
Block a user