我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django_filters.FilterSet()。
def test_global_id_field_explicit(): class ArticleIdFilter(django_filters.FilterSet): class Meta: model = Article fields = ['id'] field = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleIdFilter) filterset_class = field.filterset_class id_filter = filterset_class.base_filters['id'] assert isinstance(id_filter, GlobalIDFilter) assert id_filter.field_class == GlobalIDFormField
def test_filterset_descriptions(): class ArticleIdFilter(django_filters.FilterSet): class Meta: model = Article fields = ['id'] max_time = django_filters.NumberFilter(method='filter_max_time', label="The maximum time") field = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleIdFilter) max_time = field.args['max_time'] assert isinstance(max_time, Argument) assert max_time.type == Float assert max_time.description == 'The maximum time'
def test_global_id_multiple_field_explicit(): class ReporterPetsFilter(django_filters.FilterSet): class Meta: model = Reporter fields = ['pets'] field = DjangoFilterConnectionField(ReporterNode, filterset_class=ReporterPetsFilter) filterset_class = field.filterset_class multiple_filter = filterset_class.base_filters['pets'] assert isinstance(multiple_filter, GlobalIDMultipleChoiceFilter) assert multiple_filter.field_class == GlobalIDMultipleChoiceField
def test_global_id_multiple_field_explicit_reverse(): class ReporterPetsFilter(django_filters.FilterSet): class Meta: model = Reporter fields = ['articles'] field = DjangoFilterConnectionField(ReporterNode, filterset_class=ReporterPetsFilter) filterset_class = field.filterset_class multiple_filter = filterset_class.base_filters['articles'] assert isinstance(multiple_filter, GlobalIDMultipleChoiceFilter) assert multiple_filter.field_class == GlobalIDMultipleChoiceField
def test_free_text_filter_exception(): with pytest.raises(ImproperlyConfigured): class SearchFilterSet(django_filters.FilterSet): search = FreeTextFilter( # no fields set ) class Meta: model = question_models.Question fields = ['search']