Skip to main content

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

Project description

# 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.

## 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.

``` python
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.

```python
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:

```python
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
```


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

drf-replace-ordering-filter-1.0.1.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file drf-replace-ordering-filter-1.0.1.tar.gz.

File metadata

  • Download URL: drf-replace-ordering-filter-1.0.1.tar.gz
  • Upload date:
  • Size: 2.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for drf-replace-ordering-filter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 fd75456cb058217cbf7dbd0c651b1bf380d20a1e26a6b42bdcb70281662acc17
MD5 0f20ca10771f971d8db55bc943367b77
BLAKE2b-256 70976f7378cffc3f188368bee349597c95cf0821b82691bd46aad6860babab88

See more details on using hashes here.

File details

Details for the file drf_replace_ordering_filter-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: drf_replace_ordering_filter-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for drf_replace_ordering_filter-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7045625fb4177dea090462e2d8d3c098b56a379c5a1d314b8aa042b51b4341c
MD5 d7e1fc622a899b33fa6780075c39f516
BLAKE2b-256 92feaca194d9fe8c1da83fb0c55bed32ac3bbbc9290bce640b8ed44bcb157a74

See more details on using hashes here.

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