簡體   English   中英

按配送中心數量篩選產品渠道顧問 REST API

[英]Filter Products by Distribution Center Quantity Channel advisor REST API

我正在研究django項目,在該項目中,我使用文件導出功能從Channel Advisor平台獲取產品。 我正在使用過濾器 ' $filter=ProfileID eq 和 TotalQuantity gt 0'

https://developer.channeladvisor.com/working-with-products/product-exports

我想根據目錄列表頁面中的“可用倉庫數量”字段中的值而不是“總數量”來查詢產品。 按配送中心數量過濾,例如:“鳳凰”配送中心庫存大於 0 的產品。

謝謝你。

我認為最簡單的方法是覆蓋ModelViewSetget_queryset方法:

視圖.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 :%22book%22%7D

可以像這樣構建:

腳本.js

// using ajax as an example:
var filter = JSON.stringify({
  "name" : "book"
});

$.ajax({
   "url" : "my_website.com/api/products?filter=" + filter,
   "type" : "GET",
   ...
});

一些優點:

  • 無需指定每個視圖上可以過濾哪些字段 class
  • 一次編寫,隨處使用
  • 前端過濾看起來與 django 過濾完全相同
  • 可以用exclude做同樣的事情

一些缺點:

  • 如果您希望某些字段不可過濾,則可能存在安全風險
  • 查詢表的不太直觀的前端代碼

總的來說,這種方法對我來說比那里的任何軟件包都有用得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM