django多表过滤和搜索

首先,搜索和过滤的区别是:搜索是模糊匹配,比如在名称中按关键字来搜。
而过滤是分项目精确一致,比如同时符合几个精确条件。
模糊搜索:
warehouse_list = Warehouse.objects.filter(warehouse_name__contains=dict_arg['name'])
多条件联合检索,要用迭代的方式来进行:
if 'commodity_classification' in dict_arg:
    id_list = WarehouseCommodityClassification.objects.filter(
        commodity_classification=dict_arg['commodity_classification']).values('warehouse_id')
    print(id_list)
    warehouse_list = Warehouse.objects.filter(id__in=id_list)
if 'province' in dict_arg:
    print(dict_arg['province'])
    warehouse_list = warehouse_list.filter(province=dict_arg['province'])
if 'warehouse_mode' in dict_arg:
    warehouse_list = warehouse_list.filter(warehouse_mode=dict_arg['warehouse_mode'])
if 'min_area' in dict_arg:
    print(int(dict_arg['min_area']))
    warehouse_list = warehouse_list.filter(share_acreage__gt=int(dict_arg['min_area']))
if 'max_area' in dict_arg:
    print(int(dict_arg['max_area']))
    warehouse_list = warehouse_list.filter(share_acreage__lt=int(dict_arg['max_area']))

你可能感兴趣的:(django多表过滤和搜索)