Django-Filter DRF Camel Case Helpers
A collection of utility classes that make using camel cased query parameters easier with Django REST Framework and django-filter. Filter set query parameters can be written using conventional snake cased naming in Python code, but will be treated as camel case in the API. Additionally, schemas generated using DRF Spectacular will use the correct camel case notation.
Usage
To get the full benefit of this package, just swap out the normal DjangoFilterBackend and OrderingFilter classes
provided by the django_filter package with those implemented in django_filter_drf_camel_case.
from django.db import models
from django_filters.rest_framework import filters
from django_filters.rest_framework import filterset
from rest_framework.viewsets import ModelViewSet
from django_filter_drf_camel_case import OrderingFilter
from django_filter_drf_camel_case import DjangoFilterBackend
class Post(models.Model):
title = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_add_now=True)
follow_up = models.ForeignKey("Post", null=True, on_delete=models.SET_NULL)
class PostFilters(filterset.FilterSet):
created_at = filters.IsoDateTimeFromToRangeFilter()
sort = OrderingFilter(fields=("created_at", "title", "follow_up__title"))
class Meta:
model = Post
fields = {
"title": {"exact", "contains"},
"follow_up__title": {"exact"},
}
class PostViewSet(ModelViewSet):
queryset = Post.objects.all()
filter_backends = [DjangoFilterBackend]
filterset_class = PostFilters
The supported query parameters will be:
titlecreatedAtcreatedAt__ltauthor__fullName
The sort keys are:
createdAt/-createdAttitle/-titlefollowUp__title/-followUp__title
Underscore vs Dunderscore
To avoid ambiguous query parameters based on lookup expressions, these utilities will respect the default use of the
dunderscore (__) pattern by django-filter to separate fields from lookup expressions and relationships.
If you want to avoid this dunderscore behavior, then the recommendation is to use explicit keys, using underscores instead of dunderscores where you want. A possible alternative filterset would be
from django.db import models
from django_filters.rest_framework import filters
from django_filters.rest_framework import filterset
from django_filter_drf_camel_case import OrderingFilter
class Post(models.Model):
title = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_add_now=True)
follow_up = models.ForeignKey("Post", null=True, on_delete=models.SET_NULL)
class PostFilters(filterset.FilterSet):
created_at = filters.IsoDateTimeFromToRangeFilter()
follow_up_title = filters.CharFilter(field_name="follow_up__title")
sort = OrderingFilter(fields={
"created_at": "createdAt",
"title": "title",
"follow_up__title": "followUpTitle",
})
class Meta:
model = Post
fields = ("title",)
Resulting in the query parameters:
titlecreatedAtcreatedAtLtfollowUpTitle
The sort keys are:
createdAt/-createdAttitle/-titlefollowUpTitle/-followUpTitle
Release files for django-filter-drf-camel-case 0.1.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 | |
|---|---|---|---|
| django_filter_drf_camel_case-0.1.0.tar.gz | 4.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_filter_drf_camel_case-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.4 kB
Release files / django_filter_drf_camel_case-0.1.0.tar.gz
| Download URL | django_filter_drf_camel_case-0.1.0.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0f34a9c366a76b02e9f1ac6dcb18bcea0cec6b909093ace62cf21ec787cd1732
|
|
BLAKE2b-256 checksum How to use checksums |
67f251c488a11da984016d5964f801d785967f59e041ca0ed965bd87867aa427
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.15
|
Release files / django_filter_drf_camel_case-0.1.0-py3-none-any.whl
| Download URL | django_filter_drf_camel_case-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce936d14012a7ce5e023e0c39e3b2cbdc7f8b543d90d33898d88654329e03783
|
|
BLAKE2b-256 checksum How to use checksums |
78ee3660d4c8a211eecb80c9f33724195181a414a70c8172b758a070213b4cd7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.15
|