url_filter.filters module¶
-
class
url_filter.filters.
Filter
(source=None, *args, **kwargs)[source]¶ Bases:
object
Filter class which main job is to convert leaf
LookupConfig
toFilterSpec
.Each filter by itself is meant to be used a “field” in the
FilterSpec
.Parameters: - source (str) – Name of the attribute for which which filter applies to
within the model of the queryset to be filtered
as given to the
FilterSet
. - form_field (Field) – Instance of Django’s
forms.Field
which will be used to clean the filter value as provided in the queryset. For example if field isIntegerField
, this filter will make sure to convert the filtering value to integer before creating aFilterSpec
. - lookups (list, optional) – List of strings of allowed lookups for this filter. By default all supported lookups are allowed.
- default_lookup (str, optional) – If the lookup is not provided in the querystring lookup key,
this lookup will be used. By default
exact
lookup is used. For example the default lookup is used when querystring key isuser__profile__email
which is missing the lookup soexact
will be used. - is_default (bool, optional) – Boolean specifying if this filter should be used as a default
filter in the parent
FilterSet
. By default it isFalse
. Primarily this is used when querystring lookup key refers to a nestedFilterSet
however it does not specify which filter to use. For example lookup keyuser__profile
intends to filter something in the user’s profile however it does not specify by which field to filter on. In that case the default filter within profileFilterSet
will be used. At most, one default filter should be provided in theFilterSet
.
-
parent
¶ FilterSet
Parent
FilterSet
to which this filter is bound to
-
name
¶ str
Name of the field as it is defined in parent
FilterSet
-
bind
(name, parent)[source]¶ Bind the filter to the filterset.
This method should be used by the parent
FilterSet
since it allows to specify the parent and name of each filter within the filterset.
-
clean_value
(value, lookup)[source]¶ Clean the filter value as appropriate for the given lookup.
Parameters: - value (str) – Filter value as given in the querystring to be validated and cleaned by using appropriate Django form field
- lookup (str) – Name of the lookup
See also
-
components
¶ List of all components (source names) of all parent filtersets.
-
get_form_field
(lookup)[source]¶ Get the form field for a particular lookup.
This method does not blindly return
form_field
attribute since some lookups require to use different validations. For example for if theform_field
isCharField
but the lookup isisnull
, it makes more sense to useBooleanField
as form field.Parameters: lookup (str) – Name of the lookup Returns: Instantiated form field appropriate for the given lookup. Return type: Field
-
get_spec
(config)[source]¶ Get the
FilterSpec
for the providedconfig
.Parameters: config (LookupConfig) – Lookup configuration for which to build FilterSpec
. The lookup should be a leaf configuration otherwiseValidationError
is raised.Returns: spec constructed from the given configuration. Return type: FilterSpec
-
lookups
¶
-
root
¶ This gets the root filterset.
-
source
¶ Source field/attribute in queryset model to be used for filtering.
This property is helpful when
source
parameter is not provided when instantiatingFilter
since it will use the filter name as it is defined in theFilterSet
. For example:>>> class MyFilterSet(FilterSet): ... foo = Filter(form_field=CharField()) ... bar = Filter(source='stuff', form_field=CharField()) >>> fs = MyFilterSet() >>> print(fs.fields['foo'].source) foo >>> print(fs.fields['bar'].source) stuff
- source (str) – Name of the attribute for which which filter applies to
within the model of the queryset to be filtered
as given to the