Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用django filter应用程序中的MethodFilter?_Python_Django_Django Filters - Fatal编程技术网

Python 如何使用django filter应用程序中的MethodFilter?

Python 如何使用django filter应用程序中的MethodFilter?,python,django,django-filters,Python,Django,Django Filters,我不知道如何在django filter应用程序中使用MethodFilter,在文档()中找不到它。我需要为日期字段创建带有from和to输入的过滤器。你知道我如何使用MethodFilter或其他方法来实现这一点吗 我有一些课: class Product(django_filters.FilterSet): class Meta: model = Product fields = ['name', 'category', 'created_at]

我不知道如何在django filter应用程序中使用MethodFilter,在文档()中找不到它。我需要为日期字段创建带有from和to输入的过滤器。你知道我如何使用MethodFilter或其他方法来实现这一点吗

我有一些课:

class Product(django_filters.FilterSet):

    class Meta:
        model = Product
        fields = ['name', 'category', 'created_at]

我已经创建了字段,我想通过创建的字段(从和到)过滤产品。

我不确定我是否完全理解您的要求,但要过滤您的产品,您可以使用Q


我在GitHub中搜索并找到了这个。它起作用了

class DateRangeField(django_filters.fields.RangeField):
    # Django-Filter DateRangeFilter that really accepts a range of dates ;)
    def __init__(self, *args, **kwargs):
        fields = (
            forms.DateField(),
            forms.DateField(),
        )
        forms.MultiValueField.__init__(self, fields, *args, **kwargs)


class DateRangeFilter(django_filters.RangeFilter):
    field_class = DateRangeField
要回答问题的“如何使用方法过滤器”部分,请在
过滤器集
上定义一个方法,并将其指定为过滤器的
操作

例如,要按用户名进行筛选,您可以执行以下操作:

class F(FilterSet):
    username = MethodFilter(action='filter_username')

    class Meta:
        model = User
        fields = ['username']

    def filter_username(self, queryset, value):
        return queryset.filter(
            username=value
        )

谢谢,但这不是我的意思。我不知道如何使用django过滤器。也许,我需要使用django_filters.RangeFilter类。而不是
Products.objects.filter(Q(created_at_gte=from)&Q(created_at_ite=to)
为什么不只是
Products.objects.filter(created_at_gte=from,created_at_at_lte=to)
我可以让方法过滤器只取布尔值吗?我需要过滤字段是否为None(=false)或者别的什么事。
class F(FilterSet):
    username = MethodFilter(action='filter_username')

    class Meta:
        model = User
        fields = ['username']

    def filter_username(self, queryset, value):
        return queryset.filter(
            username=value
        )