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 to FilterSpec.

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 is IntegerField, this filter will make sure to convert the filtering value to integer before creating a FilterSpec.
  • 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 is user__profile__email which is missing the lookup so exact 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 is False. Primarily this is used when querystring lookup key refers to a nested FilterSet however it does not specify which filter to use. For example lookup key user__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 profile FilterSet will be used. At most, one default filter should be provided in the FilterSet.
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

get_form_field()

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 the form_field is CharField but the lookup is isnull, it makes more sense to use BooleanField 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 provided config.

Parameters:config (LookupConfig) – Lookup configuration for which to build FilterSpec. The lookup should be a leaf configuration otherwise ValidationError is raised.
Returns:spec constructed from the given configuration.
Return type:FilterSpec
lookups
repr(prefix=u'')[source]
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 instantiating Filter since it will use the filter name as it is defined in the FilterSet. 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