1. 程式人生 > >stark組件3_filter過濾

stark組件3_filter過濾

情況 php publish book 其它 get temp 有效 出版

過濾項

在配置類中定義,title字段沒什麽意義,這次僅作演示用

list_filter = ["title", "publish", "authors"]

調用和取數據

調用:

filter_condition = self.get_filter_condition(request)

取出數據:
連續兩個filter之間是且的關系,等於取出符合這兩個條件的數據

data_list = self.model.objects.all().filter(search_condition).filter(filter_condition)

處理邏輯

def get_filter_linktag(self):
    link_dic = {}

    #--------------------數據處理邏輯-------------------------
    #循環過濾項列表
    for filter_field in self.config.list_filter: #["title", "publish", "authors"]

        #獲取request.GET中相應是哪個字段
        #舉例:http://127.0.0.1:8000/stark/app01/book/?publish=2
        #當循環到publish時獲取這個2
        cid = self.request.GET.get(filter_field, 0)

        #根據字符串名稱拿字段對象,self.config是當前這張表的配置類實例對象
        filter_field_obj = self.config.model._meta.get_field(filter_field)
        #print("filter_field_obj==>", filter_field_obj)
        #得到app01.Book.publish和app01.Book.authors

        #拿對象下面的數據
        #.rel .to 來自於ForeignKey和ManyToManyField這兩個類
        #.rel可以看到對象的數據類型,加上.to可以拿到對象下面的數據,.to只對一對一,一對多,多對多有效
        #print(filter_field_obj.rel.to)
        #結果:
        #< class ‘app01.models.Publish‘>
        #< class ‘app01.models.Author‘>

        #判斷當前字段對象是不是一對多或多對多類型(需要提前引入這兩個類),兩種情況取得值也有所不懂
        if isinstance(filter_field_obj, ForeignKey) or isinstance(filter_field_obj, ManyToManyField):
            #如果是的話就取這個字段關聯的內容
            data_list = filter_field_obj.rel.to.objects.all() #[publish1, publish2....]
            # print("data_list==>", data_list)
            # < QuerySet[ < Publish: 圖靈新知 >, < Publish: dzm >, < Publish: 老男孩出版社 >] >
            # < QuerySet[ < Author: egon >, < Author: alex >] >
        else:#如果不是的話直接取字段對象的內容,這裏只取主鍵和當前filter_field的值,比如當前正好是"title"
            data_list = self.config.model.objects.all().values("pk", filter_field)
            #print("data_list==>", data_list)
            #data_list==> <QuerySet [{‘pk‘: 7, ‘title‘: ‘go語言第三版‘}, {‘pk‘: 8, ‘title‘: ‘linux‘}

        #----------------頁面渲染---------------
        temp = []
        #拷貝一份request.GET 用於修改
        params = copy.deepcopy(self.request.GET)

        #處理ALL標簽
        #如果params(request.GET)中有值
        if params.get(filter_field):
            #刪除字典中原有的filter_field(例如是"publish"),但保留其它字段的內容
            del params[filter_field]
            #添加html到列表,此處需要把params序列化
            temp.append(mark_safe("<a href=‘?%s‘>ALL</a>" %params.urlencode()))
        else: #如果沒有值     
            temp.append(mark_safe("<a href=‘‘>ALL</a>"))

        #處理數據標簽
        #將data_list循環起來 < QuerySet[ < Publish: 圖靈新知 >, < Publish: dzm >...] >
        for obj in data_list:
            #判斷當前字段對象是不是一對多或多對多
            if isinstance(filter_field_obj, ForeignKey) or isinstance(filter_field_obj, ManyToManyField):
                #此時的obj是一個個對象,取主鍵值和文本內容
                pk = obj.pk
                text = str(obj) #圖靈新知
                params[filter_field] = pk #結果<QueryDict: {‘publish‘: [2]}>
            else: #如果不是
                #此時的obj是QuerySet{‘pk‘: 10, ‘title‘: ‘PHP‘}
                pk = obj.get("pk")
                text = obj.get(filter_field)
                params[filter_field] = text

            #params裏面是QueryDict(<QueryDict: {‘publish‘: [2]}>),所以要做序列化
            _url = params.urlencode()

            #給選中內容加高亮
            if cid == str(pk) or cid == text:
                link_tag = "<a href=‘?%s‘ class=‘action‘>%s</a>" % (_url, text)
            else:
                link_tag = "<a href=‘?%s‘>%s</a>" % (_url, text)
            #添加到temp列表
            temp.append(mark_safe(link_tag))

        #構建字典用作返回
        link_dic[filter_field] = temp
        #{‘publish‘: ["<a href=‘‘>ALL</a>", "<a href=‘?title=go%E8%AF%AD%E8%A8%80%E7%AC%AC%E4%B8%89%E7%89%88&publish=2‘>圖靈新知</a>"...

    return link_dic

stark組件3_filter過濾