I'm trying to write a test in which an object is updated using patch.
class Search(models.Model):
id_search = models.AutoField(primary_key=True)
id_user = models.IntegerField(null=False)
.
.
archive = models.BooleanField(default=False)
def test_archive_search(self):
user = User(id_user=75720912,
login='Client:75720912',
)
user.save()
search = Search(
id_user=75720912,
.
.
archive=False
)
search.save()
url = reverse('search-update', kwargs={'id_search':1})
data = {'archive': True}
response = self.client.patch(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url(r'^search-update/(?P<id_search>\d+)$', SearchUpdateView.as_view(), name='search-update')
class SearchUpdateView(generics.UpdateAPIView):
serializer_class = SearchSerializer
def get_object(self,id_search):
return Search.objects.get(id_search=id_search)
def patch(self, request):
id_search = self.request.query_params.get('id_search', None)
search_object = self.get_object(id_search=id_search)
serializer = SearchSerializer(search_object, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)
and get this error:
TypeError: patch() got an unexpected keyword argument 'id_search'
Interesting thing is that when the url was:
url(r'^search-update/$', SearchUpdateView.as_view(), name='search-update')
SearchUpdateView worked properly with given query params.
EDIT
I discovered that passing id_search to patch in view solves this problem when it comes to test, but it spoils working view.
class SearchUpdateView(generics.UpdateAPIView):
serializer_class = SearchSerializer
def get_object(self, id_search):
return Search.objects.get(id_search=id_search)
def patch(self, request, id_search):
#id_search = self.request.query_params.get('id_search', None)
search_object = self.get_object(id_search=id_search)
serializer = SearchSerializer(search_object, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)
Still I've got no idea how to bring it together.
if you define the url that way, the patch method will get the id_search param as a keyword argument, as the error says.
Instead, you are retrieving it as if it came as a query param, i.e. not part of the url path but as search-update?id_search=.
Given you are passing None as a default when getting it, it works when you omit it.
So choose which way you want to go.
In case the url definition is correct, then add the id_search argument to the signature of the patch method
and remove the code that retrieves it manually.
Or do both, as suggested in the comments above, by assigning a default value of None to the argument and retrieving it from the request if it is not part of the path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With