url_filter.utils module¶
-
class
url_filter.utils.
FilterSpec
(components, lookup, value, is_negated=False)[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.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 be 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
.
-
-
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.
-
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