改变django-filter默认行为 [英] Altering django-filter default behaviour

查看:366
本文介绍了改变django-filter默认行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 django-filter 应用程式特定访客。

This is a django-filter app specific guestion.

有没有人试图根据条件为过滤器引入条件进行查询?

Has anyone tried to introduce conditions for the filters to query according to the condition?

让我给一个例如:

假设我们有一个产品模型。可以根据名称价格进行过滤。

Suppose we have a Product model. It can be filtered according to its name and price.

默认的 django-filter 行为是因为我们使用更多的过滤器并将它们链接在一起,因此过滤数据使用 AND 语句(缩小搜索范围)。

The default django-filter behaviour is that, as we use more filters and chain them together, they filter data using AND statements (it narrows the search).

我想更改此行为并添加一个ChoiceFilter说两个选项: AND 以及 OR 。从这一点来看,过滤器应该根据用户选择的方式工作。

I'd like to change this behaviour and add a ChoiceFilter, say with two options: AND as well as OR. From this point, the filter should work according to what a user have selected.

例如如果用户查询具有 name__startswith =Juice的产品 price__lte = 10.00 ,应列出所有名称以 Juice 开头的产品以及价格低于 10.00 。

Eg. if a user query for products with name__startswith="Juice" OR price__lte=10.00, it should list all the products with names starting with Juice as well as products with price below 10.00.

Django-filter docs说过滤器可以采取一个参数:

Django-filter docs say that the filter can take an argument:

action

An optional callable that tells the filter how to handle the queryset. It recieves a 
QuerySet and the value to filter on and should return a Queryset that is filtered 
appropriately.

这似乎是我正在寻找的,但文档没有任何进一步的解释。请问有哪些建议?

which seems to be what I am looking for, but the docs lacks any further explanation. Suggestions please?

@EDIT:

这是 views.py

This is views.py:

def product_list(request):
    f = ProductFilter(request.GET, queryset=Product.objects.all())
    return render_to_response('my_app/template.html', {'filter': f})


推荐答案

动作不会剪切。这个回调用于特定的过滤器字段,只能访问该字段的值。

action won't cut it. This callback is used for particular filter field and only has access to that field's value.

最简单的方法是创建多个小部件过滤器字段,类似于 RangeField 。查看来源

The cleanest way would be to create multi-widget filter field, similar to RangeField. Check out the source.

因此,您使用两个日期字段 name price 和逻辑键入 [AND | OR] 作为字段,这样您可以一次访问所有这些值以在自定义查询集中使用。

So instead two date fields you use name, price and the logic type [AND|OR] as fields, this way you have access to all these values at once to use in custom queryset.

编辑1:

这是一个小写,以选定的运算符查询两个字段。
https://gist.github.com/mariodev/6689472

This is a little gist I wrote to show how to query two fields with selected operator. https://gist.github.com/mariodev/6689472

用法:

class ProductFilter(django_filters.FilterSet):
    nameprice = NamePriceFilter()

    class Meta:
        model = Product
        fields = ['nameprice']

在重用方面实际上并不是非常灵活,但肯定可以重新考虑使其有用。

It's actually not very flexible in terms of re-usage, but certainly can be re-factored to make it useful.

这篇关于改变django-filter默认行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆