简体   繁体   English

patch() 得到了一个意外的关键字参数

[英]patch() got an unexpected keyword argument

I'm trying to write a test in which an object is updated using patch .我正在尝试编写一个测试,其中使用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 是:

url(r'^search-update/$', SearchUpdateView.as_view(), name='search-update')

SearchUpdateView worked properly with given query params. SearchUpdateView使用给定的查询参数正常工作。

EDIT编辑

I discovered that passing id_search to patch in view solves this problem when it comes to test, but it spoils working view.我发现将id_search传递给视图中的patch可以解决测试时的这个问题,但它破坏了工作视图。

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.如果您以这种方式定义 url,则 patch 方法将获取id_search参数作为关键字参数,如错误所述。

Instead, you are retrieving it as if it came as a query param, ie not part of the url path but as search-update?id_search= .相反,您正在检索它,好像它是作为查询参数出现的,即不是 url 路径的一部分,而是作为search-update?id_search=

Given you are passing None as a default when getting it, it works when you omit it.鉴于您在获取它时将None作为默认值传递,当您省略它时它会起作用。

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.如果 url 定义正确,则将id_search参数添加到 patch 方法的签名并删除手动检索它的代码。

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或者,如上面的评论中所建议的那样,通过为参数分配默认值 None 并从请求中检索它(如果它不是路径的一部分)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM