Package: peewee-generic-filters
Generic peewee filters.QuickstartInstall peewee filters:$pip3installpeewee_generic_filtersimportpeeweeimportpeewee_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)also it's possible to createFilterSetwithout binding concrete model.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=="")))In this case it's possible to use a FilterSet for multiple similar models.
But it is much slower than using a FilterSet with an explicit model.FiltersCharFilterThis filter does simple character matches, used withCharFieldandTextField.NumberFilterFilters based on a numerical value, used withIntegerField,FloatField, andDecimalField.DateTimeFilterMatches on a date and time. Used withDateTimeField.TimeFilterMatches on a time. Used withTimeField.DateFilterMatches on a date. Used withDateFieldby default.BooleanFilterThis filter matches a boolean, eitherTrueorFalse, used withBooleanField.UUIDFilterThis filter matches an UUID, used withBinaryUUIDField.The following are the arguments that apply to all filters:field_nameThe name of the model field that is filtered against.
If this argument is not provided, it defaults the filter’s attribute name on theFilterSetclass.
Field names can traverse relationships by joining the related parts with separator (.). e.g., a product’s manufacturer.name.descriptionFilter description. Defaults to empty string.operatorThe field lookup that should be performed in the filter call.
Should be one of the following values:eq,lt,gt,le,ge,ne,like,ilike,is_null,in,not_in,contains,startswith,endswith,regexp,iregexp.
Defaults toeq.methodForMethodFilteronly.
An argument that tells the filter how to handle the queryset.
It can accept either a callable or the name of a method on theFilterSet.
The callable receives aquery, thefield_nameof the model field to filter on, thevalueto filter with, andcontext.
It should return a filtered query. The parametervalueof a callable should have annotation.Special filtersSearchingFilterIs used for searching in multiple fields. It accepts one additional argument:fields
The list of fields for searching.OffsetFilterSpecify value for OFFSET clause.LimitFilterSpecify value for LIMIT clause.It accepts two additional arguments:defaultDefault value for LIMIT clause.
Defaults to100.maximumMaximum value for LIMIT clause.
Defaults toNone.OrderingFilterEnable queryset ordering. It accepts two additional arguments that are used to build the ordering choices:fieldsIs a mapping of {parameter name: model field name}.fieldsmay also just be a list of strings. In this case, the field names simply double as the exposed parameter names.defaultDefault ordering.SearchingFilterEnable queryset searching. It accepts one additional argument:fieldsIs a mapping of {model field name: operator}.fieldsmay also just be a list of strings.
In this case, the operator iscontains.