简体   繁体   English

django rest 框架中的自定义过滤

[英]custom filtering in django rest framework

I am trying to implement custom filtering in my code and went through the documentation.我正在尝试在我的代码中实现自定义过滤并浏览了文档。 But I couldnt understand the following snippets from the docs.但我无法理解文档中的以下片段。

class UserFilter(django_filters.FilterSet):
    class Meta:
        model = User
        fields = ['username', 'last_login']

# or

class UserFilter(django_filters.FilterSet):
    class Meta:
        model = User
        fields = {
            'username': ['exact', 'contains'],
            'last_login': ['exact', 'year__gt'],
        }

The doc says we can use either one the first or second one but what is the exact difference??医生说我们可以使用第一个或第二个,但确切的区别是什么? What is the meaning of contains and year_gt? contains和year_gt是什么意思?

What is the exact difference?确切的区别是什么?

With fields = ['username', 'last_login'] , the generated filters are:使用fields = ['username', 'last_login'] ,生成的过滤器是:

qs.filter(username='some_value') and qs.filter(last_login=some_date_value) qs.filter(username='some_value')qs.filter(last_login=some_date_value)

With

fields = {
        'username': ['exact', 'contains'],
        'last_login': ['exact', 'year__gt'],
    }

the generated filters are:生成的过滤器是:

qs.filter(username='some_value') , qs.filter(username__contains='some_value') for the username field. qs.filter(username='some_value')qs.filter(username__contains='some_value')用于username名字段。

qs.filter(last_login=some_date_value) , qs.filter(last_login__year__gt=some_year_value) for the last_login field. qs.filter(last_login=some_date_value) , qs.filter(last_login__year__gt=some_year_value)用于last_login字段。

So we can see at first time that the second produce 2 filters options for each field, and the first produce only one filter option ( exact match ) for the given value.所以我们第一次可以看到第二个为每个字段生成 2 个过滤器选项,而第一个为给定值只生成一个过滤器选项( exact match )。

What is the meaning of contains and year__gt? contains 和 year__gt 是什么意思?

contains is used to filter by a value that can be found inside a field. contains用于按可以在字段中找到的值进行过滤。 ex: dj is in django .例如: djdjango中。

year__gt in other hand is only for date field.另一方面, year__gt仅适用于日期字段。 When you have a date field like created , Django let you filter by the year , month , day by just do: .filter(created__year=2021, created__month=12) .当您有一个像created这样的日期字段时,Django 让您可以按yearmonthday过滤,只需执行以下操作: .filter(created__year=2021, created__month=12) And more possibility by .filter(created__year__gt=2020) that means the year of created field > 2020 . .filter(created__year__gt=2020)的更多可能性意味着创建字段的年份 > 2020 It can be applied to month__gt (month > 11), day__lt (day < 25).它可以应用于month__gt (月> 11), day__lt (日<25)。

There's a link in the docs there for the django lookups docs also, and what you'll find with this is that you can make searching/filtering more efficient by using that second option. django 查找文档的文档中有一个链接,您会发现,您可以通过使用第二个选项来提高搜索/过滤的效率。

The first option in the docs, where fields is just a list will do an exact match query against each of the fields you list.文档中的第一个选项,其中fields只是一个列表,将对您列出的每个字段进行完全匹配查询。

In the second option, hat the filters library will do with the fields dictionary is it'll use "double underscore" notation to create lookups of;在第二个选项中,过滤器库将使用fields字典来使用“双下划线”表示法来创建查找;

  • username__exact
  • username__contains
  • last_login__exact
  • last_login__year__gt

You can use the double underscore to access an attribute of something, or the lookup type.您可以使用双下划线来访问某物的属性或查找类型。 So on last_login for that final example above, it'll take the year of the last_login value and for that year, the lookup type is greater than ( gt ).因此,在上面最后一个示例的last_login上,它将采用last_login值的年份,并且对于那一年,查找类型大于( gt )。

There are lots of these lookup values that can be really helpful to learn.有很多这样的查找值对学习很有帮助。 For example, contains is case sensitive but you can use icontains for a case insensitive match.例如, contains区分大小写,但您可以使用icontains进行不区分大小写的匹配。 There's also lt for less than, or lte / gte for less/greater than or equal.还有lt表示小于,或lte / gte表示小于/大于或等于。 Check out the available lookups here . 在此处查看可用的查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM