peewee过滤器
peewee-generic-filters的Python项目详细描述
通用peewee筛选器。
快速启动
安装peewee过滤器:
$ pip3 install peewee_generic_filters
importpeeweeimportpeewee_filtersasfiltersclassProduct(peewee.Model):title=peewee.CharField()description=peewee.CharField(null=True)price=peewee.IntegerField()classFilter(filters.FilterSet):title=filters.Filter(operator="startswith")has_description=filters.MethodFilter("filter_description")price_min=filters.Filter(operator="ge")deffilter_description(self,query,value:bool,**kwargs):returnquery.where(Product.description.is_null(notvalue)|((Product.description!="")ifvalueelse(Product.description=="")))classMeta:model=ProductFilter({"title":"foo","has_description":True}).apply(Product)
也可以在不绑定具体模型的情况下创建FilterSet
。
classFilter(filters.FilterSet):title=filters.CharFilter(operator="startswith")has_description=filters.MethodFilter("filter_description")price_min=filters.NumberFilter(operator="ge")deffilter_description(self,query,value:bool,**kwargs):returnquery.where(Product.description.is_null(notvalue)|((Product.description!="")ifvalueelse(Product.description=="")))
在这种情况下,可以对多个类似的模型使用filterset。 但它比使用带有显式模型的filterset慢得多。
过滤器
charfilter
此筛选器执行简单的字符匹配,与CharField
和TextField
一起使用。
数字过滤器
基于数值的过滤器,与IntegerField
、FloatField
和DecimalField
一起使用。
日期时间筛选器
在日期和时间匹配。与DateTimeField
一起使用。
时间过滤器
一次匹配。与TimeField
一起使用。
日期筛选器
在某个日期匹配。默认情况下与DateField
一起使用。
布尔滤波器
此筛选器匹配与BooleanField
一起使用的True
或False
布尔值。
uuidfilter
此筛选器与用于BinaryUUIDField
的uuid匹配。
以下是适用于所有筛选器的参数:
字段名
筛选依据的模型字段的名称。
如果未提供此参数,则默认为FilterSet
类的筛选器属性名。
字段名可以通过将相关部分与分隔符(.)连接来遍历关系。例如,产品的制造商名称。
说明
筛选器说明。默认为空字符串。
操作员
应在筛选器调用中执行的字段查找。
应为以下值之一:^{lt
,gt
,le
,^{ilike
,is_null
,in
,not_in
,contains
,startswith
,endswith
,regexp
,^{regexp
,^{le
,ge
,^{eq
。
方法
仅适用于MethodFilter
。
告诉筛选器如何处理queryset的参数。
它可以接受FilterSet
上的可调用方法或方法名。
可调用的接收一个query
,要筛选的模型字段的field_name
,要筛选的value
,以及context
。
它应该返回经过筛选的查询。可调用的参数value
应该有注释。
特殊过滤器
搜索筛选器
用于在多个字段中搜索。它接受一个附加参数:
字段 要搜索的字段列表。
偏移滤波器
为OFFSET子句指定值。
极限滤波器
指定LIMIT子句的值。
它接受两个附加参数:
默认值
LIMIT子句的默认值。
默认为100
。
None
。订购过滤器
启用QuerySet排序。它接受两个用于构建排序选项的附加参数:
字段
是{parameter name:model field name}的映射。fields
也可能只是字符串列表。在这种情况下,字段名只需加倍作为公开的参数名。
默认值
默认顺序。
搜索筛选器
启用QuerySet搜索。它接受一个附加参数:
字段
是{model field name:operator}的映射。fields
也可能只是字符串列表。
在这种情况下,运算符是contains
。