Skip to main content

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

Tests codecov License: MIT Source Code

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


Download files

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

Source Distribution

django_filter_drf_camel_case-0.1.0.tar.gz (4.7 kB view hashes)

Uploaded Source

Built Distribution

Supported by

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