gpt4 book ai didi

django - 如何在 django rest 框架中复制请求

转载 作者:行者123 更新时间:2023-12-05 04:14:52 25 4
gpt4 key购买 nike

假设我有这段代码

class SnippetList(APIView):
"""
List all snippets, or create a new snippet.
"""
def get(self, request, format=None):
snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True)
return Response(serializer.data)

现在让我们假设在我的 get 中,我需要修改 request.data

    def get(self, request, format=None):
data = modify(request.data)
request.data = data # This don't work as i can't set attribute on request
response = self.post(self, new_request, format=None) # i want to do post method now with new request
serializer = SnippetSerializer(snippets, many=True)

有什么办法吗

最佳答案

你可以尝试这样的事情:

def get(self, request, format=None):
response = self.post(self, request, format=None, data=data ) # i want to do post method now with new request
serializer = SnippetSerializer(snippets, many=True)

现在您的方法帖子应该是:

def post(self, request, format=None, **kwargs):
original_data = request.data
additional_data = kwargs.get('data') # now you will be able to do whatever you want
# more code goes here

关于django - 如何在 django rest 框架中复制请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33927437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com