Django-filter自定义查询条件

django-filter 使用总结

持续更新....

  • 自定义时间区间类型
  • 可以调用request,根据条件自定义筛选返回queryset
from django_filters import FilterSet, OrderingFilter, CharFilter, NumberFilter, DateTimeFilter

from worklogs.models import ReportList


class ReportsListFilter(FilterSet):
    content = CharFilter(lookup_expr='icontains')
    start_time = DateTimeFilter(field_name='create_time', lookup_expr='gte')
    end_time = DateTimeFilter(field_name='create_time', lookup_expr='lt')
    create_time = DateTimeFilter(field_name='create_time',)
    log_type = CharFilter(field_name='log_type', method="filter_log_type")

    def filter_log_type(self, queryset, name, value):
        params = self.request.query_params.getlist("log_type")

        queryset = ReportList.objects.filter(log_type__in=params)
       
        return queryset

    class Meta:
        model = ReportList
        fields = ['log_type', 'content']

 

你可能感兴趣的:(技术点,django框架)