def get_filter_class(self, view, queryset=None): """ Return the django-filters `FilterSet` used to filter the queryset. """ filter_class = getattr(view, 'filter_class', None) filter_fields = getattr(view, 'filter_fields', None) new_filter_fields = [] if filter_fields: for field in filter_fields: if settings.REST_FRAMEWORK["INITCAP_FIELDS"]: new_filter_fields.append(unconvert(field)) else: new_filter_fields.append(field) if filter_class: filter_model = filter_class.Meta.model assert issubclass(filter_model, queryset.model), \ 'FilterSet model %s does not match queryset model %s' % \ (filter_model, queryset.model) return filter_class if filter_fields: class AutoFilterSet(self.default_filter_set): class Meta: model = queryset.model fields = new_filter_fields order_by = True return AutoFilterSet return None
def filter_queryset(self, request, queryset, view): search_fields = getattr(view, 'search_fields', None) new_search_fields = [] if search_fields: for field in search_fields: if settings.REST_FRAMEWORK["INITCAP_FIELDS"]: new_search_fields.append(unconvert(field)) else: new_search_fields.append(field) if not new_search_fields: return queryset orm_lookups = [self.construct_search(str(search_field)) for search_field in new_search_fields] for search_term in self.get_search_terms(request): or_queries = [models.Q(**{orm_lookup: search_term}) for orm_lookup in orm_lookups] queryset = queryset.filter(reduce(operator.or_, or_queries)) return queryset