python - 405 error when custom-defined PATCH method is called for Django REST APIView -
i making api call in test client:
response2 = self.client.patch('/object/update/%d/' % object_id, {'object_attribute':4})
the relevant serializer , view class object:
class objectupdateserializer(serializers.modelserializer): class meta: model = object include=('object_attribte','another_attribute',) class objectview(apiview): def patch(self, request, pk, format=none): obj = object.objects.get(id=pk) data = request.data.copy() """do stuff data here...""" serializer = objectupdateserializer(instance = obj, data=data, partial=true) if serializer.is_valid(raise_exception=true): serializer.save() return response(serializer.data,status.http_200_ok)
i able work put method when used that, wanted have api call methods more in-line methods mean (so patch partial replacement). however, response test client call above this:
{u'detail': u'method "patch" not allowed.'}
which 405 error (method not allowed).
i checked see if there issues django 1.10, , got output in django shell:
>>> django.views.generic import view >>> view.http_method_names [u'get', u'post', u'put', u'patch', u'delete', u'head', u'options', u'trace']
it appears if isn't issue django's settings, i've set up. issue here?
i faced same problem. appeared it's not allowed use different views same route:
urlpatterns = [ # ... url('^sessions/?$', views.tokendelete.as_view()), # viewer has delete() url('^sessions/?$', views.tokenedit.as_view()), # viewer has patch() ]
the correct way is:
urlpatterns = [ # ... url('^sessions/?$', views.token.as_view()), # viewer has both patch() , delete() ]
Comments
Post a Comment