如果有N多fitler条件,
单独放在一个长语句里显然不好看。
还好,
django支持字典方式的过滤条件,
写法大约与单独的长语里差不多。
如下:
def get_queryset(self): filter_dict = dict() if self.request.query_params.get(‘site_name‘): filter_dict[‘site_name__name‘] = self.request.query_params.get(‘site_name‘) if self.request.query_params.get(‘order_no‘): filter_dict[‘order_no‘] = self.request.query_params.get(‘order_no‘) if self.request.query_params.get(‘version_name‘): if self.request.query_params.get(‘version_name‘) == "null": filter_dict[‘version_name__isnull‘] = True else: filter_dict[‘version_name__name‘] = self.request.query_params.get(‘version_name‘) # 以下过滤发布单的环境及时间(30天) # filter_dict[‘deploy_progress‘] = u"待发布" filter_dict[‘deploy_status__in‘] = ["UAT", "PRD", "SIM", "DRP", "BUILD"] current_date = timezone.now() filter_dict[‘change_date__gt‘] = current_date - timedelta(days=3000) return DeployPool.objects.filter(**filter_dict)
时间: 2024-10-10 15:49:04