python - Django/Python Where to find a list of available filter functions when querying
问题描述
I'm currently working with a Python Django project and in there I filter data from PostgreSQL where I need to find the functions that can be called after each field followed by two underscores.
Ex:
- birthday__year, birthday__month
- start_time__hour, start_time__seconds
- salary__gte, salary__lt
Like wise from where can we get a list of all possibilities (except for foreign key fields) that can be called through the TestObj.objects.filter() ?
解决方案
These are called field lookups [Django-doc]. You can find a list of builtin field lookups in the documentation on querysets. When writing this answer, the current list of builtin lookups is:
__exact
;__iexact
;__contains
;__icontains
;__in
;__gt
;__gte
;__lt
;__lte
;__startswith
;__istartswith
;__endswith
;__iendswith
;__range
;__date
;__year
;__iso_year
;__month
;__day
;__week
;__week_day
;__quarter
;__time
;__hour
;__minute
;__second
;__isnull
;__regex
; and__iregex
.
Note that you can implement your own field lookup [Django-doc] as well. Indeed, you can define a lookup and how to map that to a database query. Certain Django packages for example might provide an extra set of field lookups.
The set of lookups is thus not "fixed". You can add lookups if you often need to retrieve/filter a certain condition, and there exists no builtin lookup for that.
推荐阅读
- r - 如何创建具有给定平均最短路径和平均度值的图/网络?
- html - 标记表情符号不呈现
- c - 接受来自用户的二进制字符串并检查其在 C 中的有效性?
- android - 错误:程序类型已存在:android.arch.lifecycle.Transformations
- angular - 页面上未显示编辑和删除按钮
- elasticsearch - Elasticsearch如何在映射中指定索引为false的关键字数组?
- c++ - 分段错误 - 链表实现
- python - Behave `before_scenario` 钩子不记得初始化的变量值
- excel - 在计算机上未安装 Access 2016 的情况下从 Excel 调用访问函数
- angularjs - 使用 ui-sref 和 $state.go 在新选项卡中打开