如何从 generics.ListAPIView DRF 中的 post 请求获取数据?
How to get data from post request in generics.ListAPIView DRF?
有观点:
class Search(CategoryDetail):
serializer_class = ProductSerializer
def get_queryset(self):
# print(self.request)
return self.get_filters(None)
它继承自另一个 ListAPIView 并做了一些事情。
从前面 Vue.js 到 post 我传递了一些查询参数:
async Search(){
axios
.post('api/products/search', {'query': this.query})
.then(response =>{
this.products = response.data
})
.catch(error=>{
console.log(error)
})
}
最初,我通过搜索视图功能执行此 post 请求的工作:
@api_view(['POST'])
def search(request):
query = request.data.get('query')
if query:
products = Product.objects.filter(Q(name__icontains=query) | Q(description__icontains = query))
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)
else:
return Response({"products": []})
但现在我需要将视图函数更改为 class,但在 class 中我需要获得相同的查询 = request.data.get('query') , 但是,当我尝试时,不断返回 Bad Request 错误。
ListAPIView
不接受 post 请求。您应该将查询作为 GET
请求查询参数传递,并在 get_queryset
方法中访问它以过滤结果。
class ProductList(generics.ListCreateAPIView):
serializer_class = ProductSerializer
def get_queryset(self):
query = self.request.query_params.get('query')
if query is not None:
return Product.objects.filter(Q(name__icontains=query) | Q(description__icontains = query))
else:
Product.objects.none()
如果您必须使用 POST
数据和 return 对象列表,您可以实现自己的 APIView
并使用它的 post
方法。
class PrdocutList(APIView):
def post(self, request, format=None):
query = request.data.get('query')
if query:
products = Product.objects.filter(Q(name__icontains=query) | Q(description__icontains = query))
else:
products = Product.objects.none()
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)
您也可以实现自己的 filter_backend。
有观点:
class Search(CategoryDetail):
serializer_class = ProductSerializer
def get_queryset(self):
# print(self.request)
return self.get_filters(None)
它继承自另一个 ListAPIView 并做了一些事情。
从前面 Vue.js 到 post 我传递了一些查询参数:
async Search(){
axios
.post('api/products/search', {'query': this.query})
.then(response =>{
this.products = response.data
})
.catch(error=>{
console.log(error)
})
}
最初,我通过搜索视图功能执行此 post 请求的工作:
@api_view(['POST'])
def search(request):
query = request.data.get('query')
if query:
products = Product.objects.filter(Q(name__icontains=query) | Q(description__icontains = query))
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)
else:
return Response({"products": []})
但现在我需要将视图函数更改为 class,但在 class 中我需要获得相同的查询 = request.data.get('query') , 但是,当我尝试时,不断返回 Bad Request 错误。
ListAPIView
不接受 post 请求。您应该将查询作为 GET
请求查询参数传递,并在 get_queryset
方法中访问它以过滤结果。
class ProductList(generics.ListCreateAPIView):
serializer_class = ProductSerializer
def get_queryset(self):
query = self.request.query_params.get('query')
if query is not None:
return Product.objects.filter(Q(name__icontains=query) | Q(description__icontains = query))
else:
Product.objects.none()
如果您必须使用 POST
数据和 return 对象列表,您可以实现自己的 APIView
并使用它的 post
方法。
class PrdocutList(APIView):
def post(self, request, format=None):
query = request.data.get('query')
if query:
products = Product.objects.filter(Q(name__icontains=query) | Q(description__icontains = query))
else:
products = Product.objects.none()
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)
您也可以实现自己的 filter_backend。