url_filter.filtersets.base module¶
-
class
url_filter.filtersets.base.FilterSet(data=None, queryset=None, context=None, strict_mode=None, *args, **kwargs)[source]¶ Bases:
url_filter.filters.BaseFilterMain user-facing classes to use filtersets.
It primarily does:
- takes queryset to filter
- takes querystring data which will be used to filter given queryset
- from the querystring, it constructs a list of
LookupConfig - loops over the created configs and attempts to get
FilterSpecfor each - in the process, it delegates the job of constructing spec to child filters when any match is found between filter defined on the filter and lookup name in the config
Parameters: - data (QueryDict, optional) –
QueryDictof querystring data. Only optional whenFilterSetis used as a nested filter within anotherFilterSet. - queryset (iterable, optional) – Can be any iterable as supported by the filter backend.
Only optional when
FilterSetis used as a nested filter within anotherFilterSet. - context (dict, optional) – Context for filtering. This is passed to filtering backend.
Usually this would consist of passing
requestandviewobject from the Django view. - strict_mode (str, optional) – Strict mode how
FilterSetshould behave when any validation fails. Seeurl_filter.constants.StrictModedoc for more information. Default isempty.
-
default_filter¶ Cached property for looking up default filter. Default filter is a filter which is defined with
is_default=True. Useful when lookup config references nested filter without specifying which field to filter. In that case default filter will be used.
-
default_strict_mode= u'empty'¶ Default strict mode which should be used when one is not provided in initialization.
-
filter()[source]¶ Main method which should be used on root
FilterSetto filter queryset.This method:
- asserts that filtering is being done on root
FilterSetand that all necessary data is provided - creates
LookupConfigfrom the provided data (querystring) - loops over all configs and attempts to get
FilterSetfor all of them - instantiates filter backend
- uses the created filter specs to filter queryset by using specs
Returns: Filtered queryset Return type: querystring - asserts that filtering is being done on root
-
filter_backend¶ Property for getting instantiated filter backend.
Primarily useful when accessing filter_backend outside of the filterset such as leaf filters or integration layers since backend has useful information for both of those examples.
-
filter_backend_class¶
-
filter_options_class¶ Class to be used to construct
MetaduringFilterSetclass creation time in its metaclass.alias of
FilterSetOptions
-
filters¶ Cached property for accessing filters available in this filteset. In addition to getting filters via
get_filters(), this property binds all filters to the filterset by usingBaseFilter.bind().See also
-
get_filter_backend()[source]¶ Get instantiated filter backend class.
This backend is then used to actually filter queryset.
-
get_filters()[source]¶ Get all filters defined in this filterset.
By default only declared filters are returned however this method is meant to be used as a hook in subclasses in order to enhance functionality such as automatically adding filters from model fields.
-
get_spec(config)[source]¶ Get
FilterSpecfor the givenLookupConfig.If the config is non leaf config (it has more nested fields), then the appropriate matching child filter is used to get the spec. If the config however is a leaf config, then
default_filteris used to get the spec, when available, and if not, this filter is skipped.Parameters: config (LookupConfig) – Config for which to generate FilterSpecReturns: Individual filter spec Return type: FilterSpec
-
get_specs()[source]¶ Get list of
FilterSpecfor the given querystring data.This function does:
- unpacks the querystring data to a list of
LookupConfig - loops through all configs and uses appropriate children
filters to generate list of
FilterSpec - if any validations fails while generating specs,
all errors are collected and depending on
strict_modeit re-raises the errors or ignores them.
Returns: List of FilterSpecReturn type: list - unpacks the querystring data to a list of
-
repr(prefix=u'')[source]¶ Custom representation of the filterset
Parameters: prefix (str) – Prefix with which each line of the representation should be prefixed with. This allows to recursively get the representation of all descendants with correct indentation (children are indented compared to parent)
-
validate_key(key)[source]¶ Validate that
LookupConfigkey is correct.This is the key as provided in the querystring. Currently key is validated against a regex expression.
Useful to filter out invalid filter querystring pairs since not whole querystring is not dedicated for filter purposes but could contain other information such as pagination information. In that case if the key is invalid key for filtering, we can simply ignore it without wasting time trying to get filter specification for it.
Parameters: key (str) – Key as provided in the querystring
-
class
url_filter.filtersets.base.FilterSetOptions(options=None)[source]¶ Bases:
objectBase class for handling options passed to
FilterSetviaMetaattribute.
-
class
url_filter.filtersets.base.ModelFilterSetOptions(options=None)[source]¶ Bases:
url_filter.filtersets.base.FilterSetOptionsCustom options for
FilterSetused for model-generated filtersets.-
fields¶ None, list, optional – Specific model fields for which filters should be created for. By default it is
Nonein which case for all fields filters will be created for.
-
exclude¶ list, optional – Specific model fields for which filters should not be created for.
bool, optional – Whether related/nested fields should be allowed when model fields are automatically determined (e.g. when explicit
fieldsis not provided).
-
extra_kwargs¶ dict, optional – Additional kwargs to be given to auto-generated individual filters
-