class CourseFilterSet(FilterSet): # 课程的价格范围要大于min_price,小于max_price min_price = filters.NumberFilter(field_name='price', lookup_expr='gt') max_price = filters.NumberFilter(field_name='price', lookup_expr='lt') class Meta: model=models.Course fields=['course_category']
class ComputerFilterSet(FilterSet): from django_filters import filters min_price = filters.NumberFilter(field_name="price", lookup_expr="gte") max_price = filters.NumberFilter(field_name="price", lookup_expr="lte") class Meta: model = Computer fields = ["brand", "min_price", "max_price"]
class UserTagFilter(filterset.FilterSet): created_date__lte = filters.DateTimeFilter(field_name="created_date", lookup_expr="lte") created_date__gte = filters.DateTimeFilter(field_name="created_date", lookup_expr="gte") user = filters.NumberFilter(field_name="user") album = filters.NumberFilter(field_name="photo__album")
class AnswerFilter(django_filters.rest_framework.FilterSet): application = filters.NumberFilter(field_name="application_id__id") question = filters.NumberFilter(field_name="question_id__id") class Meta: model = Answer fields = '__all__'
class CourseFilterSet(FilterSet): min_price = filters.NumberFilter(field_name="price", lookup_expr="gte") # 可以根据价格查询,小于或等于这个价格 max_price = filters.NumberFilter(field_name="price", lookup_expr="lte") class Meta: model = models.Course fields = ["course_category","students"] # 还可以根据分类查询与热度查询
class EvaluationFilter(django_filters.rest_framework.FilterSet): application_id = filters.NumberFilter(field_name="application_id__id") user_id = filters.NumberFilter(field_name="user_id__id") class Meta: model = Evaluation fields = '__all__'
class QnABoardCommentFilter(django_filters.rest_framework.FilterSet): user_id = filters.NumberFilter(field_name="user_id__id") board_id = filters.NumberFilter(field_name="board__id") class Meta: model = models.QnABoardComment fields = '__all__'
class PhotoFilter(filterset.FilterSet, TagsFilterMixin): created_date__lte = filters.DateTimeFilter(field_name="created_date", lookup_expr="lte") created_date__gte = filters.DateTimeFilter(field_name="created_date", lookup_expr="gte") album = filters.NumberFilter(field_name="album") photographer = filters.NumberFilter(field_name="photographer")
class StudyBoardFilter(django_filters.rest_framework.FilterSet): group_id = filters.NumberFilter(field_name="group_id__id") user_id = filters.NumberFilter(field_name="user_id__id") class Meta: model = models.StudyBoard fields = '__all__'
class ProductFilter(FilterSet): min_price = filters.NumberFilter(field_name="price", lookup_expr='gte') min_weight = filters.NumberFilter(field_name="weight", lookup_expr='gte') class Meta: model = ProductSets fields = ['min_price', 'min_weight']
class ProductFilter(FilterSet): min_price = filters.NumberFilter(field_name='price', lookup_expr='gte') max_price = filters.NumberFilter(field_name="price", lookup_expr='lte') class Meta: model = Product fields = ['min_price', 'max_price']
class CourseFilterSet(FilterSet): max_price = filters.NumberFilter(field_name='price', lookup_expr='lte') min_price = filters.NumberFilter(field_name='price', lookup_expr='gte') class Meta: model = models.Course fields = ['course_category', 'max_price', 'min_price']
class ComputerFilterSet(FilterSet): min_price = filters.NumberFilter(field_name="price", lookup_expr="gte") max_price = filters.NumberFilter(field_name="price", lookup_expr="lte") class Meta: model = Computer fields = ("price", "min_price", "max_price")
class MeasurementValueFilter(filters.FilterSet): assay = django_filters.ModelChoiceFilter( name='measurement__assay', queryset=models.Assay.objects.all(), ) created_before = django_filters.IsoDateTimeFilter( name='updated__mod_time', lookup_expr='lte', ) created_after = django_filters.IsoDateTimeFilter( name='updated__mod_time', lookup_expr='gte', ) line = django_filters.ModelChoiceFilter( name='measurement__assay__line', queryset=models.Line.objects.all(), ) x__gt = django_filters.NumberFilter(name='x', lookup_expr='0__gte') x__lt = django_filters.NumberFilter(name='x', lookup_expr='0__lte') y__gt = django_filters.NumberFilter(name='y', lookup_expr='0__gte') y__lt = django_filters.NumberFilter(name='y', lookup_expr='0__lte') class Meta: model = models.MeasurementValue fields = {'measurement': ['exact', 'in']}
class CarFilterSet(FilterSet): min_price = filters.NumberFilter(field_name='price', lookup_expr='gte') max_price = filters.NumberFilter(field_name='price', lookup_expr='lte') class Meta: model = models.Car fields = ['brand', 'min_price', 'max_price']
class CourseFilterSet(FilterSet): # 实现区间:field_name关联model表属性,lookup_expr设置过滤规则 min_price = filters.NumberFilter(field_name='price', lookup_expr='gte') max_price = filters.NumberFilter(field_name='price', lookup_expr='lte') class Meta: model = models.Course # min_price 和 max_price 自定义规则字段可以不在fields中配置 fields = ['course_category']
class KmGridScoreFilter(KmGridFilter): total_score = filters.NumberFilter(field_name='total_score') min_total_report = filters.NumberFilter(field_name='total_report', lookup_expr='gte') max_total_report = filters.NumberFilter(field_name='total_report', lookup_expr='lte') class Meta: model = KmGridScore fields = KmGridFilter.Meta.fields + ['total_score', 'min_total_report', 'max_total_report']
class KmGridFilter(GeoFilterSet): max_population = filters.NumberFilter(field_name='population', lookup_expr='lte') min_population = filters.NumberFilter(field_name='population', lookup_expr='gte') contains_geom = GeometryFilter(field_name='geometry', lookup_expr='contains') class Meta: model = KmGrid fields = ['max_population', 'min_population', 'contains_geom']
class ComputerFilterSet(FilterSet): from django_filters import filters min_price = filters.NumberFilter(field_name="price", lookup_expr='gte') max_price = filters.NumberFilter(field_name='price', lookup_expr='lte') class Meta: model = Computer fields = ['brand', 'min_price', 'max_price']
class CarAutoFilterSet(FilterSet): # 根据自己想要的字段过来,可以区间查询 price_max = filters.NumberFilter(field_name="price", lookup_expr="gte") price_min = filters.NumberFilter(field_name="price", lookup_expr="lte") class Meta: model = models.Car fields = ['brand', "price_min", "price_max"]
class ProductFilter(django_filters.FilterSet): title = django_filters.CharFilter(lookup_expr='icontains', label='عنوان محصول') category = CustomModelMultipleChoiceFilter_cat( field_name='category__slug', to_field_name='slug', queryset=Category.objects.all(), conjoined=False, label='دستهبندی') variants__color = CustomModelMultipleChoiceFilter( field_name='variation__color', to_field_name='color', queryset=Variants.objects.all(), conjoined=False, label='رنگها') variants__size = CustomModelMultipleChoiceFilter( field_name='variation__size', to_field_name='size', queryset=Variants.objects.all(), conjoined=False, label='سایزها') brand = CustomModelMultipleChoiceFilter(field_name='brand__slug', to_field_name='slug', queryset=Brand.objects.all(), conjoined=False, label='برندها') min_price = filters.NumberFilter(field_name="price", lookup_expr='gte', label='حداقل قیمت') max_price = filters.NumberFilter(field_name="price", lookup_expr='lte', label='حداکثر قیمت') amount = filters.NumberFilter(field_name='amount', lookup_expr='gte', label='کالاهای موجود') order_by = filters.CharFilter(field_name="order_by", method='filter_order_by', label='مرتب شده براساس') def filter_order_by(self, queryset, name, value): if value: return queryset.order_by(value) return queryset class Meta: model = Product fields = [ 'title', 'brand', 'category', 'variants__color', 'variants__size', 'min_price', 'max_price', 'amount', 'order_by' ]
class NovelFilterSet(FilterSet): max_works = filters.NumberFilter(field_name='total_words', lookup_expr='lte') min_works = filters.NumberFilter(field_name='total_words', lookup_expr='gte') class Meta: model = models.Novel fields = [ 'category', 'novel_status', 'max_works', 'min_works', 'is_vip' ]
class TransactionFilterSet(filterset.FilterSet): account = filters.ModelChoiceFilter(queryset=account_queryset) category = filters.ModelChoiceFilter(queryset=category_queryset) tags = filters.CharFilter(method="filter_by_tags") created_at = filters.CharFilter(method="filter_by_date_range") updated_at = filters.CharFilter(method="filter_by_date_range") ordering = filters.OrderingFilter(fields=ORDERING_FIELDS, field_labels=ORDERING_LABELS, label=_("Ordering")) min_amount = filters.NumberFilter(field_name="amount", lookup_expr="gte", label=_("Minimum amount")) max_amount = filters.NumberFilter(field_name="amount", lookup_expr="lte", label=_("Maximum amount")) description = filters.CharFilter(lookup_expr="icontains", label=_("Descripton")) class Meta: model = Transaction fields = ("type", ) form = TransactionFilterForm def filter_by_tags(self, queryset, field_name, tags): if self.form.is_valid(): tag_values = [tag["value"] for tag in tags] queryset = queryset.filter(**{ "author": self.request.user, f"{field_name}__name__in": tag_values }) return queryset def filter_by_date_range(self, queryset, name, value): if self.form.is_valid(): start_date, end_date = value.split(" - ") date_field = forms.DateField() start_datetime = datetime.combine( date=date_field.clean(start_date), time=time.min, tzinfo=pytz.UTC) end_datetime = datetime.combine(date=date_field.clean(end_date), time=time.max, tzinfo=pytz.UTC) queryset = queryset.filter( **{ "author": self.request.user, f"{name}__gte": start_datetime, f"{name}__lte": end_datetime, }) return queryset
class HistoryFilter(FilterSet): date = filters.DateFilter(field_name='date__date', help_text='date of change | ' 'format: %Y-%m-%d %H:%M:%S') feature_id = filters.NumberFilter(field_name='feature_id', help_text='id of feature') user_id = filters.NumberFilter(field_name='user_id', help_text="id of user which changed") attribute = filters.CharFilter(field_name='attribute', help_text="name of changeable attribute") class Meta: model = History fields = ['feature_id', 'user_id', 'date', 'attribute']
class SessionFilter(django_filters.rest_framework.FilterSet): user_id = filters.NumberFilter(field_name="user_id__id") class Meta: model = models.Session fields = '__all__' exclude = ['session_file']
class StudyGroupFilter(django_filters.rest_framework.FilterSet): group_name = filters.NumberFilter(field_name="name__id") class Meta: model = StudyGroup exclude = ['img'] fields = '__all__'
class SeveralFieldsFilter(FilterSet): text = filters.CharFilter(lookup_expr='icontains') decimal = filters.NumberFilter(lookup_expr='lt') date = filters.DateFilter(lookup_expr='gt') class Meta: model = FilterableItem fields = ['text', 'decimal', 'date']
class MeasurementValueFilter(filters.FilterSet): assay = django_filters.ModelChoiceFilter( field_name="measurement__assay", queryset=models.Assay.objects.all()) created_before = django_filters.IsoDateTimeFilter( field_name="updated__mod_time", lookup_expr="lte") created_after = django_filters.IsoDateTimeFilter( field_name="updated__mod_time", lookup_expr="gte") line = django_filters.ModelChoiceFilter( field_name="measurement__assay__line", queryset=models.Line.objects.all()) x__gt = django_filters.NumberFilter(field_name="x", lookup_expr="0__gte") x__lt = django_filters.NumberFilter(field_name="x", lookup_expr="0__lte") y__gt = django_filters.NumberFilter(field_name="y", lookup_expr="0__gte") y__lt = django_filters.NumberFilter(field_name="y", lookup_expr="0__lte") class Meta: model = models.MeasurementValue fields = {"measurement": ["exact", "in"]}
class TitlesFilter(django_filters.rest_framework.FilterSet): genre = filters.CharFilter(field_name="genre") category = filters.CharFilter(field_name="category") year = filters.NumberFilter(field_name='year') name = filters.CharFilter(field_name='name', lookup_expr='contains') class Meta: model = Titles fields = ['genre', 'category', 'year', 'name']
class ProductFilterSet(FilterSet): city = filters.AllValuesMultipleFilter('seller__city') min_price = filters.NumberFilter(field_name="unit_price", lookup_expr='gte') max_price = filters.NumberFilter(field_name="unit_price", lookup_expr='lte') category = filters.AllValuesMultipleFilter(field_name="category__slug", method='filter_category') def filter_category(self, queryset, name, categories): return queryset.filter(category__slug__in=categories) sort = filters.OrderingFilter(fields=['created', 'quantity', 'unit_price']) class Meta: model = Product fields = ['category', 'city', 'min_price', 'max_price']