Giter Site home page Giter Site logo

drf-replace-ordering-filter's Introduction

drf-replace-ordering-filter

Django Rest Framework OrderingFilter backend to replace field name in ordering params.

This package is useful if you have custom hidden fields in you model to help with a better ordering.

Install

pip install drf-replace-ordering-filter

And in setting.py file of your Django's project, register the package in INSTALLED_APPS.

INSTALLED_APPS = [
  ...
  'drf_replace_ordering_filter',
  ...
]

Usage

You can user ReplaceFieldOrderingFilter as a Default Filter Backend in Rest Framework setting or specifying in a certain APIView.

ReplaceFieldOrderingFilter works in the same way as rest_framework.filters.OrderingFilter, if you do not define fields in a View to be replaced.

1. As a default setting

First of all, add ReplaceFieldOrderingFilter to DEFAULT_FILTER_BACKENDS in setting.py file of your Django's project.

REST_FRAMEWORK = {
  'DEFAULT_FILTER_BACKENDS': (
    'drf_replace_ordering_filter.filters.ReplaceFieldOrderingFilter',
  )
}

Lastly, in a view that you want to change some ordering filed to another, add the following attribute.

from rest_framework import views


class MyAPIView(views.APIView):
  replace_ordering_fields = {
    'field_to_be_replaced': 'new_field'
  }

  def get(self, request, *args, **kwargs):
    pass

2. In a specific view

The second option is using ReplaceFieldOrderingFilter only in a specfic view or viewset.

For example:

from drf_replace_ordering_filter.filters import ReplaceFieldOrderingFilter
from rest_framework import views


class MyAPIView(views.APIView):
  filter_backends = (ReplaceFieldOrderingFilter,)
  replace_ordering_fields = {
    'field_to_be_replaced': 'new_field'
  }

  def get(self, request, *args, **kwargs):
    pass

drf-replace-ordering-filter's People

Contributors

emilsas avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.