Example #1
0
class ProductFilter(FilterSet):
    # explicit filter declaration
    max_price = NumberFilter(field_name="price",
                             lookup_expr='lte',
                             label='highest price')
    max_sub_price = NumberFilter(field_name="subproduct__sub_price",
                                 lookup_expr='lte')
    sub = NumberFilter(field_name="subproduct", lookup_expr='exact')
    int_id = NumberFilter(method='filter_method_typed')
    number_id = NumberFilter(method='filter_method_untyped',
                             help_text='some injected help text')
    number_id_ext = NumberFilter(method=external_filter_method)
    # implicit filter declaration
    subproduct__sub_price = NumberFilter()  # reverse relation
    other_sub_product__uuid = UUIDFilter()  # forward relation
    # special cases
    ordering = OrderingFilter(
        fields=('price', 'in_stock'),
        field_labels={
            'price': 'Price',
            'in_stock': 'in stock'
        },
    )
    in_categories = BaseInFilter(field_name='category')
    is_free = BooleanFilter(field_name='price', lookup_expr='isnull')
    price_range = RangeFilter(field_name='price')
    model_multi_cat = ModelMultipleChoiceFilter(field_name='category',
                                                queryset=Product.objects.all())
    model_single_cat = ModelChoiceFilter(field_name='category',
                                         queryset=Product.objects.all())
    all_values = AllValuesFilter(field_name='price')

    custom_filter = CustomBooleanFilter(field_name='price',
                                        lookup_expr='isnull')
    custom_underspec_filter = CustomBaseInFilter(field_name='category')

    model_multi_cat_relation = ModelMultipleChoiceFilter(
        field_name='other_sub_product', queryset=OtherSubProduct.objects.all())

    price_range_vat = RangeFilter(field_name='price_vat')
    price_range_vat_decorated = extend_schema_field(OpenApiTypes.INT)(
        RangeFilter(field_name='price_vat'))

    class Meta:
        model = Product
        fields = [
            'category',
            'in_stock',
            'max_price',
            'max_sub_price',
            'sub',
            'subproduct__sub_price',
            'other_sub_product__uuid',
        ]

    def filter_method_typed(self, queryset, name, value: int):
        return queryset.filter(id=int(value))

    def filter_method_untyped(self, queryset, name, value):
        return queryset.filter(id=int(value))  # pragma: no cover
Example #2
0
class BookFilter(filters.FilterSet):
    author = ModelMultipleChoiceFilter(field_name="authors",
                                       queryset=Author.objects.all())

    class Meta:
        model = Book
        fields = ["name", "publication_year", "edition", "author"]
Example #3
0
class ActivityHistoryStatsFilter(FilterSet):
    group = ModelChoiceFilter(queryset=groups_queryset)
    user = ModelMultipleChoiceFilter(queryset=users_queryset, field_name='users')
    date = IsoDateTimeFromToRangeFilter(field_name='activity__date__startswith')

    class Meta:
        model = History
        fields = ['group', 'user']
Example #4
0
class BooksListFilter(FilterSet):
    """
    Кастомный фильтр для BooksViewSet:
    выбор категорий,
    выбор автора,
    выбор библиотеки
    """
    categories = ModelMultipleChoiceFilter(queryset=Categories.objects.all())
    author = ModelChoiceFilter(queryset=Authors.objects.all())
    lib_available__library = ModelChoiceFilter(
        queryset=Libraries.objects.all())

    class Meta:
        model = Books
        fields = ['categories', 'author', 'lib_available__library']
Example #5
0
class RangeFiltering(FilterSet):
    """
    A filter class for applying filters.
    """

    from django_filters.rest_framework import (
        NumberFilter,
        ModelMultipleChoiceFilter,
        CharFilter,
        DateFilter,
    )
    from .models import TransactionMode

    start_date = DateFilter(field_name="transaction_date", lookup_expr="gte")
    end_date = DateFilter(field_name="transaction_date", lookup_expr="lte")
    start_amount = NumberFilter(field_name="amount", lookup_expr="gte")
    end_amount = NumberFilter(field_name="amount", lookup_expr="lte")
    mode = ModelMultipleChoiceFilter(field_name="mode",
                                     queryset=TransactionMode.objects.all())
    amount = NumberFilter(field_name="amount")
    id = NumberFilter(field_name="id")
    category = CharFilter(field_name="category")
    transaction_date = DateFilter(field_name="transaction_date")

    class Meta:
        from .models import TransactionDetail

        model = TransactionDetail
        fields = (
            "start_date",
            "end_date",
            "start_amount",
            "end_amount",
            "mode",
            "amount",
            "id",
            "category",
            "transaction_date",
        )
Example #6
0
class RangeFiltering(FilterSet):
    """
    A filter class for applying filters.
    """
    from django_filters.rest_framework import NumberFilter, ModelMultipleChoiceFilter, CharFilter, DateFilter
    from .models import TransactionMode

    start_date = DateFilter(field_name='transaction_date', lookup_expr='gte')
    end_date = DateFilter(field_name='transaction_date', lookup_expr='lte')
    start_amount = NumberFilter(field_name='amount', lookup_expr='gte')
    end_amount = NumberFilter(field_name='amount', lookup_expr='lte')
    mode = ModelMultipleChoiceFilter(field_name='mode', queryset=TransactionMode.objects.all())
    amount = NumberFilter(field_name='amount')
    id = NumberFilter(field_name='id')
    category = CharFilter(field_name='category')
    transaction_date = DateFilter(field_name='transaction_date')

    class Meta:
        from .models import TransactionDetail

        model = TransactionDetail
        fields = ('start_date', 'end_date', 'start_amount', 'end_amount', 'mode', 'amount', 'id', 'category',
                  'transaction_date')
Example #7
0
    class MyFilter(FilterSet):
        reltd = ModelMultipleChoiceFilter(field_name="reltd", label="reltd")

        class Meta:
            model = TestModel
            fields = ["reltd"]