url_filter.utils module¶
-
class
url_filter.utils.
FilterSpec
(components, lookup, value, is_negated=False, filter_callable=None)[source]¶ Bases:
object
Class for describing filter specification.
The main job of the
FilterSet
is to parse the submitted lookups into a list of filter specs. A list of these specs is then used by the filter backend to actually filter given queryset. That’s whatFilterSpec
provides - a way to portably define filter specification to be used by a filter backend.The reason why filtering is decoupled from the
FilterSet
is because this allows to implement filter backends not related to Django.-
components
¶ list – A list of strings which are names of the keys/attributes to be used in filtering of the queryset. For example lookup config with key
user__profile__email
will have components of ``[‘user’, ‘profile’, ‘email’].
-
lookup
¶ str – Name of the lookup how final key/attribute from
components
should be compared. For example lookup config with keyuser__profile__email__contains
will have a lookupcontains
.
-
value
¶ Value of the filter.
-
is_negated
¶ bool, optional – Whether this filter should be negated. By default its
False
.
-
filter_callable
¶ func, optional – Callable which should be used for filtering this filter spec. This is primaliry meant to be used by
CallableFilter
.
-
is_callable
¶ Property for getting whether this filter specification is for a custom filter callable
-
-
class
url_filter.utils.
LookupConfig
(key, data)[source]¶ Bases:
object
Lookup configuration which is used by
FilterSet
to create aFilterSpec
.The main purpose of this config is to allow the use if recursion in
FilterSet
. Each lookup key (the keys in the querystring) is parsed into a nested one-key dictionary which lookup config stores.For example the querystring:
?user__profile__email__endswith=gmail.com
is parsed into the following config:
{ 'user': { 'profile': { 'email': { 'endswith': 'gmail.com' } } } }
-
key
¶ str – Full lookup key from the querystring. For example
user__profile__email__endswith
-
data
¶ dict, str – Either:
- nested dictionary where the key is the next key within
the lookup chain and value is another
LookupConfig
- the filtering value as provided in the querystring value
- nested dictionary where the key is the next key within
the lookup chain and value is another
Parameters: - key (str) – Full lookup key from the querystring.
- data (dict, str) – A regular vanilla Python dictionary.
This class automatically converts nested
dictionaries to instances of
LookupConfig
. Alternatively a filtering value as provided in the querystring.
-
as_dict
()[source]¶ Converts the nested
LookupConfig
to a regulardict
.
-
is_key_value
()[source]¶ Check if this
LookupConfig
is not a nestedLookupConfig
but instead the value is a non-dict value.
-
name
¶ If the
data
is nestedLookupConfig
, this gets its first lookup key.
-
value
¶ If the
data
is nestedLookupConfig
, this gets its first lookup value which could either be anotherLookupConfig
or actual filtering value.
-
-
class
url_filter.utils.
SubClassDict
[source]¶ Bases:
dict
Special-purpose
dict
with special getter for looking up values by finding matching subclasses.This is better illustrated in an example:
>>> class Klass(object): pass >>> class Foo(object): pass >>> class Bar(Foo): pass >>> mapping = SubClassDict({ ... Foo: 'foo', ... Klass: 'klass', ... }) >>> print(mapping.get(Klass)) klass >>> print(mapping.get(Foo)) foo >>> print(mapping.get(Bar)) foo