A django app to apply filters on rest endpoints
Project description
drf-query-filter
drf-query-filter is used to design fast and user pre defined queries with the 'query' found in the request url.
Table of contents
Installation
drf-query-filter can be installed using pip
pip install drf-query-filter
Usage
QuickStart
In settings.py:
INSTALLED_APPS = [
...,
'drf_query_filter',
...
]
In views:
...
from rest_framework import viewsets
from drf_query_filter import fields, filters
...
class ExampleViewSet(viewsets.GenericViewSet[Any]):
...
filter_backends = [filters.QueryParamFilter]
query_param = [
fields.StringField('id', 'id') & fields.StringField('user_id', 'user__id'),
fields.RangeDateTimeField('date_created', 'element__date_created'),
]
Fields
The view requires the definition the fields to be used in the filter. There are two ways of doing this.
By attribute:
query_params = [
fields.StringField('id') & fields.StringField('username', 'username__icontains'),
fields.ConcatField('full_name', ['first_name', V(' '), 'last_name'])
]
By callable:
def get_query_params(self) -> list[Node]:
return [
fields.StringField('id') & fields.StringField('username', 'username__icontains'),
fields.ConcatField('full_name', ['first_name', V(' '), 'last_name'])
]
The first value of the Field constructor is the name of the query in the request. For example:
/path/to/somewhere/?username=value
With the query parameter target_fields of the Field one can tell which are the target fields of the model.
Not assigning the target_field will assume that the name of the field is the same for the name of the target field.
fields.StringField('username') # it will user `username` as the target field.
To tell what target_field it is use the param target_fields, using only a str will target only one field in the model.
fields.StringField('search', 'username')
Using a list or a tuple will target multiple fields of the model.
fields.StringField('search', ['username', 'first_name', 'last_name'], connector=Q.OR)
Meaning that the result in the field search (in this case) will be assigned to all the target fields.
How does it work?
With the following fields arraigned like this:
query_params = [
fields.Field('id') & fields.Field('username', 'user__username__icontains'),
fields.RangeDateTimeField('date_created', equal=True) | fields.BooleanField('vip', 'vip_status'),
fields.ConcatField('full_name', ['user__first_name', V(' '), 'user__last_name'], lookup='icontains'),
]
Is equivalent to the following lines of code: (if all values are found in the request):
queryset = queryset.filter(
Q(id=f'{query_params["id"]}') &
Q(user__username__icontains=f'{request.query_params["username"]}')
)
queryset = queryset.filter(
Q(
date_created__gte=f'{request.query_params["date_created"]}',
date_created__lte=f'{request.query_params["date_created"]}'
) | Q(vip_status=f'{request.query_params["vip"]}')
)
queryset = queryset.annotate(
first_name_last_name=Concat('user__first_name', V(' '), 'user__last_name')
).filter(
Q(first_name_last_name__icontains=f'{request.query_params["full_name"]}')
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drf_query_filter-0.2.0.tar.gz.
File metadata
- Download URL: drf_query_filter-0.2.0.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fd3c985b6266877d0a01ed5d973d5a11574990a0ea9df6f82ebf655df47fa63
|
|
| MD5 |
c9f925c2e3afe1b98dcba3d7748c22e8
|
|
| BLAKE2b-256 |
5401155599ee20cba2212d97e60743ed54ef70197a914254f147ca83595cfe25
|
Provenance
The following attestation bundles were made for drf_query_filter-0.2.0.tar.gz:
Publisher:
python-publish.yml on Jmillan-Dev/drf-query-filter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
drf_query_filter-0.2.0.tar.gz -
Subject digest:
8fd3c985b6266877d0a01ed5d973d5a11574990a0ea9df6f82ebf655df47fa63 - Sigstore transparency entry: 1218091475
- Sigstore integration time:
-
Permalink:
Jmillan-Dev/drf-query-filter@6afdf005a37ce62c315a4d35001848833f39f6a7 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/Jmillan-Dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6afdf005a37ce62c315a4d35001848833f39f6a7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file drf_query_filter-0.2.0-py3-none-any.whl.
File metadata
- Download URL: drf_query_filter-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c01761acb6a272eec747d08ff769a87ba2701a9b10619dbac1abc9ee6764a2cb
|
|
| MD5 |
f56adfaacf6774bf791c812f006bdca2
|
|
| BLAKE2b-256 |
cbb9ccf64104d13b6403289579132ddf1d42744e1861137e13c66f1f4de3314a
|
Provenance
The following attestation bundles were made for drf_query_filter-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on Jmillan-Dev/drf-query-filter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
drf_query_filter-0.2.0-py3-none-any.whl -
Subject digest:
c01761acb6a272eec747d08ff769a87ba2701a9b10619dbac1abc9ee6764a2cb - Sigstore transparency entry: 1218091534
- Sigstore integration time:
-
Permalink:
Jmillan-Dev/drf-query-filter@6afdf005a37ce62c315a4d35001848833f39f6a7 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/Jmillan-Dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6afdf005a37ce62c315a4d35001848833f39f6a7 -
Trigger Event:
release
-
Statement type: