我正在从事一个django项目,在该项目中,我使用文件导出功能从通道顾问平台获取产品。我使用的是过滤器'$filter=ProfileID eq和TotalQuantity gt 0'。
https://developer.channeladvisor.com/working-with-products/product-exports
我想根据目录列表页面中的‘Qty效仓库’字段中的值来查询产品,而不是'TotalQuantity‘。或按配送中心数量过滤,如:“凤凰”配送中心库存大于0的产品。
谢谢。
发布于 2021-06-02 15:23:44
在我看来,最简单的方法是重写您的get_queryset方法的ModelViewSet
views.py
def BaseAPIView(...):
''' base view for other views to inherit '''
def get_queryset(self):
queryset = self.queryset
# get filter request from client:
filter_string = self.request.query_params.get('filter')
# apply filters if they are passed in:
if filters:
filter_dictionary = json.loads(filter_string)
queryset = queryset.filter(**filter_dictionary)
return queryset请求url现在将类似于,例如:my_website.com/api/products?filter={"name":"book"}
或者更准确地说:my_website.com/api/products?filter=%7B%22name%22:%22book%22%7D
它可以建造如下:
script.js
// using ajax as an example:
var filter = JSON.stringify({
"name" : "book"
});
$.ajax({
"url" : "my_website.com/api/products?filter=" + filter,
"type" : "GET",
...
});的一些优势:
everywhere
exclude所做的一样
的一些缺点:
如果希望某些字段是non-filterable
,则
总的来说,这种方法对我来说比任何软件包都有用得多。
https://stackoverflow.com/questions/67807386
复制相似问题