django-simplefilters
This is a drop-in replacement for built-in Django Rest Framework filters package. It provides an easy way to retrieve params from the URL and use them to narrow results of the queryset.
The main difference with other solutions, i.e. django-filter, is that we do not try to handle each case and actual filtering is left for the user to implement. This might sound counterintuitive for a filtering library but we believe that in many cases it's much easier to write something like
class EntryFilterSet(filters.FilterSet):
@filters.CharFilter(many=True)
def filter_status(self, queryset, values):
return queryset.filter(status__in=values)
than try to find this special case (multiple values) in your filtering library documentation.
Installation
$ pip install django-simplefilters
Usage
Let's imagine we have an Entry model with following attributes:
status_choices = ((c, c) for c in ['draft', 'published', 'archived'])
class Entry(models.Model):
title = models.CharField(max_length=64)
status = models.CharField(max_length=16, choices=status_choices)
modified_at = models.DateTimeField(auto_now=True)
and we want to allow owner to filter entries by their status and modification date.
We need to define our filterset first:
# entries/filtersets.py
import simplefilters as filters
class Entry(filters.FilterSet):
@filters.CharFilter()
def filter_status(self, queryset, value):
return queryset.filter(status=value)
@filters.DateTimeFilter()
def filter_modified_at_min(self, queryset, ts):
return queryset.filter(modified_at__gte=ts)
@filters.DateTimeFilter()
def filter_modified_at_max(self, queryset, ts):
return queryset.filter(modified_at__lte=ts)
Mostly this should be self-explanatory, however important bit is how we indicate url param. This is achieved similarly to validation in serializers: name of the parameter is prefixed with filter_ string. So, code above defines filters for following URL parameters: status, modified_at_min and modified_at_max.
Then hook it at corresponding ViewSet class:
# entries/views.py
from . import filtersets
from . import models
import simplefilters as filters
class EntryViewSet(ModelViewSet):
serializer_class = ...
filter_backends = [filters.DjangoFilterBackend]
filter_class = filtersets.Entry
class Meta:
model = models.Entry
now user would be able to perform queries like:
GET /entries?status=draft
GET /entries?modified_at_min=2018-03-30T14:00Z
etc.
Supported filters
By definition, whatever user puts as query param is simply a string. Thus, CharFiled is simplest and most basic filter. But sometimes we would need to accept other types, i.e. numbers, timestamps or flags.
Here is a full list of available filters:
CharFilter
Most basic filter. Nothing fancy is done here.
IntegerFilter
Param is casted to integer.
FlagFilter
Param is casted to bool. Strings that would be treated as True indicator (case insensitive): yes, y, true, t and 1. Similarly, those would be treated as False: no, n, false, f and 0.
DateTimeFilter
Param is casted to datetime object.
Multiple params
Sometimes we want to pass multiple params from the url. In example, we might want to allow users to filter by multiple status values and perform something like:
GET /entries?status=draft&status=archived
For this to work, simply indicate that we want to use many values at the filter method definition:
@filters.CharFilter(many=True)
def filter_status(self, queryset, values):
return queryset.filter(status__in=values)
Metadata
Release files for django-simplefilters 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-simplefilters-1.0.0.tar.gz | 4.5 kB | Without provenance | Details for django-simplefilters-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_simplefilters-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for django_simplefilters-1.0.0-py3-none-any.whl |
Total release size: 9.5 kB
Release files / django-simplefilters-1.0.0.tar.gz
| Download URL | django-simplefilters-1.0.0.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a9fbebc7a6c14c8835d687b125831d3de6ff857ad6192c8d4c6044e01e4d0828
|
|
BLAKE2b-256 checksum How to use checksums |
43009f43ce3c9708b2f77f41a2ded6488012d7ad396329ff85c26821220b3fc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|
Release files / django_simplefilters-1.0.0-py3-none-any.whl
| Download URL | django_simplefilters-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4200ee4273df294fadf762ea498a173efdff1bf0943e1b06549cb652f4bff577
|
|
BLAKE2b-256 checksum How to use checksums |
687c78029c6d9ad04c3d666a883e5070ed10af7258cafefb4044e2b7da64c0ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|
Security for django-simplefilters 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.