Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_query_filter-0.2.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_query_filter-0.2.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

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

Hashes for drf_query_filter-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8fd3c985b6266877d0a01ed5d973d5a11574990a0ea9df6f82ebf655df47fa63
MD5 c9f925c2e3afe1b98dcba3d7748c22e8
BLAKE2b-256 5401155599ee20cba2212d97e60743ed54ef70197a914254f147ca83595cfe25

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file drf_query_filter-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_query_filter-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c01761acb6a272eec747d08ff769a87ba2701a9b10619dbac1abc9ee6764a2cb
MD5 f56adfaacf6774bf791c812f006bdca2
BLAKE2b-256 cbb9ccf64104d13b6403289579132ddf1d42744e1861137e13c66f1f4de3314a

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page