A collection of utility classes that make using camel cased query parameters easier with Django REST Framework and django-filter
Project description
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:
title
createdAt
createdAt__lt
author__fullName
The sort keys are:
createdAt
/-createdAt
title
/-title
followUp__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:
title
createdAt
createdAtLt
followUpTitle
The sort keys are:
createdAt
/-createdAt
title
/-title
followUpTitle
/-followUpTitle
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
File details
Details for the file django_filter_drf_camel_case-0.1.0.tar.gz
.
File metadata
- Download URL: django_filter_drf_camel_case-0.1.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f34a9c366a76b02e9f1ac6dcb18bcea0cec6b909093ace62cf21ec787cd1732 |
|
MD5 | 57881d2c08c0717b11dd94d8826ebbfb |
|
BLAKE2b-256 | 67f251c488a11da984016d5964f801d785967f59e041ca0ed965bd87867aa427 |
File details
Details for the file django_filter_drf_camel_case-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: django_filter_drf_camel_case-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce936d14012a7ce5e023e0c39e3b2cbdc7f8b543d90d33898d88654329e03783 |
|
MD5 | fdb1434b4b55effa25bf117c6be87979 |
|
BLAKE2b-256 | 78ee3660d4c8a211eecb80c9f33724195181a414a70c8172b758a070213b4cd7 |