Giter VIP home page Giter VIP logo

django-filter-drf-camel-case's Introduction

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

django-filter-drf-camel-case's People

Contributors

camuthig avatar dependabot[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

georgy-komarov

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.