django_ufilter.utils module¶
- class django_ufilter.utils.FilterSpec(components, lookup, value, is_negated=False, filter_callable=None)[source]¶
Bases:
objectClass for describing filter specification.
The main job of the
FilterSetis 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 whatFilterSpecprovides - a way to portably define filter specification to be used by a filter backend.The reason why filtering is decoupled from the
FilterSetis because this allows to implement filter backends not related to Django.- components¶
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__emailwill have components of['user', 'profile', 'email'].- Type
list
- lookup¶
Name of the lookup how final key/attribute from
componentsshould be compared. For example lookup config with keyuser__profile__email__containswill have a lookupcontains.- Type
str
- value¶
Value of the filter.
- is_negated¶
Whether this filter should be negated. By default its
False.- Type
bool, optional
- filter_callable¶
Callable which should be used for filtering this filter spec. This is primaliry meant to be used by
CallableFilter.- Type
func, optional
- property is_callable¶
Property for getting whether this filter specification is for a custom filter callable
- class django_ufilter.utils.LookupConfig(key, data)[source]¶
Bases:
objectLookup configuration which is used by
FilterSetto 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¶
Full lookup key from the querystring. For example
user__profile__email__endswith- Type
str
- data¶
Either:
nested dictionary where the key is the next key within the lookup chain and value is another
LookupConfigthe filtering value as provided in the querystring value
- Type
dict, str
- 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
LookupConfigto a regulardict.
- is_key_value()[source]¶
Check if this
LookupConfigis not a nestedLookupConfigbut instead the value is a non-dict value.
- property name¶
If the
datais nestedLookupConfig, this gets its first lookup key.
- property value¶
If the
datais nestedLookupConfig, this gets its first lookup value which could either be anotherLookupConfigor actual filtering value.
- class django_ufilter.utils.SubClassDict[source]¶
Bases:
dictSpecial-purpose
dictwith 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
- django_ufilter.utils.dict_pop(key, d)[source]¶
Pop key from dictionary and return updated dictionary